本帖最后由 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.
源自:http://acm.zju.edu.cn/ 2812题 ACM趣题天天练,热烈欢迎论坛各位程序达人果断AC。每天前十名AC并提交自己代码都有红包相送!更有可能认识更多热爱编程喜欢编程的朋友!期待你的代码! 注:如果有提交代码的请在编辑中使用高级—>添加代码段 |
[技术| 编程·课件·Linux] ACM趣题天天练 1 Quicksum
simon3322
· 发布于 2012-08-20 08:55
· 2123 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。
本帖最后由 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; } } |
评分
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); } } } |
评分
[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; } |
评分
[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; } |
评分
庆祝电脑回来了。。。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; } |
评分
[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] |
评分
正在学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"); } |