[技术| 编程·课件·Linux] ACM趣题天天练 1 Quicksum

simon3322 · 发布于 2012-08-20 08:55 · 2123 次阅读
997
本帖最后由 simon3322 于 2012-8-20 09:06 编辑

Quicksum

Time Limit: 2 Seconds      Memory Limit: 65536 KB
A checksum is an algorithm that scans a packet of data and returns a single  number. The idea is that if the packet is changed, the checksum will also  change, so checksums are often used for detecting transmission errors,  validating document contents, and in many other situations where it is necessary  to detect undesirable changes in data.
For this problem, you will implement a checksum algorithm called Quicksum. A  Quicksum packet allows only uppercase letters and spaces. It always begins and  ends with an uppercase letter. Otherwise, spaces and letters can occur in any  combination, including consecutive spaces.
A Quicksum is the sum of the products of each character's position in the  packet times the character's value. A space has a value of zero, while letters  have a value equal to their position in the alphabet. So, A=1, B=2, etc.,  through Z=26. Here are example Quicksum calculations for the packets "ACM" and "MID CENTRAL":
        ACM: 1*1  + 2*3 + 3*13 = 46
        MID CENTRAL: 1*13 + 2*9 + 3*4 + 4*0 + 5*3 + 6*5 + 7*14 + 8*20 + 9*18 + 10*1 + 11*12 = 650
Input: The input consists of one or more packets followed by a line  containing only # that signals the end of the input. Each packet is on a line by  itself, does not begin or end with a space, and contains from 1 to 255  characters.
Output: For each packet, output its Quicksum on a separate line in the  output.
Example Input:Example Output:
ACM
MID CENTRAL
REGIONAL PROGRAMMING  CONTEST
ACN
A C M
ABC
BBC
#
46
650
4690
49
75
14
15

源自:http://acm.zju.edu.cn/ 2812题

ACM趣题天天练,热烈欢迎论坛各位程序达人果断AC。每天前十名AC并提交自己代码都有红包相送!更有可能认识更多热爱编程喜欢编程的朋友!期待你的代码!

注:如果有提交代码的请在编辑中使用高级—>添加代码段





评分

参与人数 2学分 +11 收起 理由
admin + 6 感谢您为软院筒子们提供有用信息!
service + 5 欢迎再接再厉!

查看全部评分

共收到 20 条回复
maxOrder石 · #2 · 2012-8-20 10:01:54  回复 支持 反对
本帖最后由 maxOrder石 于 2012-8-20 10:36 编辑

检测出non-zero exit code 求建议
[C++] 纯文本查看 复制代码
#include<stdio.h>
#define Max 20
int main()
{
   int value(char);
   int lines[Max]={0};
   char c;
   int i=1;
   int position=1;
   printf("intput:\n");
   while((c=getchar())!='#')
   {
      if(' '==c||c>'=A'&&c<='Z'&&c!='\n')
      {
        lines[i]=lines[i]+position*value(c);
        position++;
      }
      else
        if('\n'==c)
        {
           position=1;
           i++;
        }
        else
        {
          printf("error:input maybe include other invalid words\n");
          break;
        }
   }
  printf("output:\n");
  int j=1;
  for(;j<i;j++)
  {
   printf("%d\n",lines[j]);
  }
   

}
int value(char c)
{
   if(' '==c)
   {
    return 0;
    }
    else
   {
    return c-'A'+1;
   }
}


点评

我用gcc的,然后手打出来的,我运行没错啊  详情 回复 发表于 2012-8-20 10:34
第15行我这边编译时候报错,请再查一下  详情 回复 发表于 2012-8-20 10:18

评分

参与人数 1学分 +12 收起 理由
simon3322 + 12 赞一个!

查看全部评分

maxOrder石 · #3 · 2012-8-20 10:06:30  回复 支持 反对
那个什么时间是什么?还有memory?我的怎么看?
simon3322 · #4 · 2012-8-20 10:18:47  回复 支持 反对
maxOrder石 发表于 2012-8-20 10:01
楼主,你看怎么样?还行的话,记得给奖励哦!
[mw_shl_code=cpp,true]#include
#define Max 20

第15行我这边编译时候报错,请再查一下

点评

lines=lines+... 原来用虚拟机的gcc,然后手打上去,固然出错了  详情 回复 发表于 2012-8-20 10:35
maxOrder石 · #5 · 2012-8-20 10:23:12  回复 支持 反对
浙大检测说non-zero exit  code

点评

不清楚啊,你可以在浙大网上先AC  详情 回复 发表于 2012-8-20 10:30
maxOrder石 · #6 · 2012-8-20 10:30:13  回复 支持 反对
能替我解释一遍要求吗?
simon3322 · #7 · 2012-8-20 10:30:49  回复 支持 反对
maxOrder石 发表于 2012-8-20 10:23
浙大检测说non-zero exit  code

不清楚啊,你可以在浙大网上先AC
maxOrder石 · #8 · 2012-8-20 10:34:02  回复 支持 反对
maxOrder石 发表于 2012-8-20 10:01
检测出non-zero exit code 求建议
[mw_shl_code=cpp,true]#include
#define Max 20

我用gcc的,然后手打出来的,我运行没错啊
maxOrder石 · #9 · 2012-8-20 10:35:45  回复 支持 反对
本帖最后由 maxOrder石 于 2012-8-20 10:41 编辑
simon3322 发表于 2012-8-20 10:18
第15行我这边编译时候报错,请再查一下

lines中括号i=lines中括号i+...
原来用虚拟机的gcc,然后手打上去,固然出错了
extlpf · #10 · 2012-8-20 11:54:34  回复 支持 反对
ZOJ上已AC

[Java] 纯文本查看 复制代码
import java.util.Scanner;

public class Main {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int quicksum;
                String pkg;
                char ch; int code;
                while(sc.hasNext())
                {
                        pkg = sc.nextLine();
                        if(pkg.charAt(0) == '#') break;
                        quicksum = 0;
                        for(int i = 0; i < pkg.length(); i++)
                        {
                                ch = pkg.charAt(i);
                                if(ch == ' ')
                                {
                                        code = 0;
                                }
                                else
                                {
                                        code = (int)(ch - 'A' + 1);
                                }
                                quicksum += code * (i + 1);
                        }
                        System.out.println(quicksum);
                }
        }
}

评分

参与人数 2学分 +15 收起 理由
sunhongbo + 6 真棒,加分,欢迎积极参与。。。
simon3322 + 9 精彩回复!加分!

查看全部评分

yel_hb · #11 · 2012-8-20 12:56:37  回复 支持 反对
[C++] 纯文本查看 复制代码
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str;
	int i,len,sum;
	while(getline(cin,str))
	{
		if(str[0] == '#')
		{
			break;
		}
		else
		{
			sum = 0;
			len = str.length();
			for(i = 0; i < len; ++i)
			{
				if(str[i] != ' ')
				{
					sum = sum + (i + 1) * (str[i] - 64);
				}
			}

			cout << sum << endl;
		}
	}
	return 0;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

参与人数 1学分 +9 收起 理由
simon3322 + 9

查看全部评分

maxOrder石 · #12 · 2012-8-20 12:59:22  回复 支持 反对
我也太面向过程了
sunhongbo · #13 · 2012-8-20 16:58:04  回复 支持 反对
自从复试不要求上机之后还久。。好久没练过ACM了。。罪过
lavorange · #14 · 2012-8-20 19:04:09  回复 支持 反对
红包送什么
来自:软院网·中科大 Android客户端来自: Android客户端
寄居蟹a · #15 · 2012-8-20 20:58:57  回复 支持 反对
extlpf 发表于 2012-8-20 11:54
ZOJ上已AC

[mw_shl_code=java,true]import java.util.Scanner;

哇~~~很给力嘛!
primi · #16 · 2012-8-20 21:48:22  回复 支持 反对
ZOJ好慢啊,提交十几分钟了还不出结果
primi · #17 · 2012-8-20 22:04:08  回复 支持 反对
[C++] 纯文本查看 复制代码
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str;
    int ans;
    while(getline(cin, str))
    {
        if(str == "#")
            break;
        ans = 0;
        for(int i = 0; i < str.length(); i++)
            if(str[i] <= 90 && str[i] >= 65)
                ans += (i + 1) * (str[i] - 'A' + 1);
        cout<<ans<<endl;
    }
    return 0;
}

评分

参与人数 1学分 +6 收起 理由
simon3322 + 6

查看全部评分

WXL · #18 · 2012-8-20 23:23:19  回复 支持 反对
额,看到这亚历山大~
fghhslk · #19 · 2012-8-21 00:59:07  回复 支持 反对
庆祝电脑回来了。。。AC一记
[C++] 纯文本查看 复制代码
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str;
	int sum;
	while(getline(cin,str)) {
		sum = 0;
		if (str == "#")
			break;
		for (int i = 0; i < str.length(); i++) {
			if (str[i] < 'A' && str[i] > 'Z')
				cout << "input error!" <<endl;
			if (str[i] != ' ') {
				sum += (str[i] - 'A' + 1) * (i + 1);
			}
		}
		cout << sum << endl;
	}
	return 0;
}

评分

参与人数 1学分 +6 收起 理由
simon3322 + 6

查看全部评分

justcx · #20 · 2012-8-21 23:30:05  回复 支持 反对
[AppleScript] 纯文本查看 复制代码
#include<iostream>
using namespace std;
int main()
{
char c[256];
int sum=0;
while(cin.getline(c,256))
{
if(c[0]=='#')
break;
for(int i=0;c[i]!='\0';i++)
{
if(c[i]!=' ')
sum+=(i+1)*(c[i]-64);
}
cout<<sum<<endl;
sum=0;
}
return 0;
}


在家没事做,感谢楼主,已AC[img]file:///C:/Documents%20and%20Settings/Administrator/Application%20Data/Tencent/Users/894114106/QQ/WinTemp/RichOle/7B0%25QGB5WU]4%25VTI4281@5W.jpg[/img]

评分

参与人数 1学分 +3 收起 理由
simon3322 + 3

查看全部评分

rxgalpha · #21 · 2012-8-22 15:48:53  回复 支持 反对
正在学perl,来个perl版本的~
[Perl] 纯文本查看 复制代码
#!/user/bin/perl
use warnings;
use diagnostics;

while(<STDIN>){
	chomp;
	last if($_ eq '#');
	my $sum;
	for(my $i=1;$i<=length;$i++){
		next if charAt($_,$i-1)=~/^\s$/;
		$sum+=$i*(ord(charAt($_,$i-1))-ord('A')+1);
	}
	message($sum);
}

sub charAt{
	my ($str, $n) = @_;
	return substr($str,$n,1);
}
sub message{
    my $m = shift or return;
    print("$m\n");
}



评分

参与人数 1学分 +3 收起 理由
simon3322 + 3

查看全部评分

回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表