hdu 2734(Quicksum)
2015-07-28 10:48
489 查看
Quicksum(原网址:http://acm.hdu.edu.cn/showproblem.php?pid=2734)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2893 Accepted Submission(s): 2127
[align=left]Problem Description[/align]
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 = 46MID CENTRAL: 1*13 + 2*9 + 3*4 + 4*0 + 5*3 + 6*5 + 7*14 + 8*20 + 9*18 + 10*1 + 11*12 = 650
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each packet, output its Quicksum on a separate line in the output.
[align=left]Sample Input[/align]
ACM MID CENTRAL REGIONAL PROGRAMMING CONTEST ACN A C M ABC BBC #
[align=left]Sample Output[/align]
46 650 4690 49 75 14 15
题意:
计算一串字符的“Quicksum”的结果。计算规则为每一个字符的(字母表位置*在单词中的位置)之和,如果是空格则为零。(例:‘A C M’=75:‘A’在字母表中是第1个,在这个单词中也是第1个,所以‘A’=1*1=1;第二个字符是空格,记为零;‘C’在字母表中是第3个,在这个单词中也是第3个,所以‘C’=3*3=9;第四个字符是空格,记为零;’M'在字母表中是第13个,在这个单词中是第5个,所以'M'=13*5=65;所以‘A C M’=1+0+9+0+65=75)。(多组数据,出现#结束)
参考代码:
#include<iostream> #include<cstring> using namespace std; int main() { char a[300]={0}; int i,la; long long s; cin.getline(a,256,'\n');//输入一行(包括空格) while(a[0]!='#') { la=strlen(a); s=0; for(i=0;i<la;i++) { if(a[i]!=' ')//空格直接跳过 s+=(((int)a[i]-64)*(i+1));//注:我乘i+1是因为循环从0开始 } cout<<s<<endl; cin.getline(a,256,'\n');//输入一行(包括空格) } return 0; }
运行结果:
Accepted | 2734 | 15MS | 1932K | 515B |
这道题不算很难。易错点在cin.getline和计算上。
cin.getline不再讲了(见/article/8209854.html)
计算需要用到强制转换和ASCII码。ASCII码中大写'A'为65,大写'B'为66,以此类推,大写'Z'为90。题目中确保所给数据为大写字母,因此只需将字母强制转换为ASCII码再减去64就能得到它的字母表位置。
参考知识:
cin.getline(见hdu 1062(Text Reverse)).
ASCII码(American Standard Code for Information Interchange,美国标准信息交换代码)——》百度。
相关文章推荐
- iPad开发 - UIPopoverController控制器
- Android Studio finished with non-zero exit value 1
- ios 使用xib时,在UIScrollView中添建内容view时,使用约束的注意
- hdu 杭电1509 Windows Message Queue【优先队列】
- 高逼格UI-ASD(Android Support Design)
- 实现登录弹出窗口
- UIImageView 圆角设置
- pywinauto处理UI自动化
- HDU 2818 Building Block (并查集)
- 五种创建UIImage的类方法
- Android 学习笔记 Contacts (一)ContentResolver query 参数详解
- HDU 1711 Number Sequence
- ANDROID L——Material Design详细解释(UI控制)
- UINavigationController返回手势失效问题
- Unable to resolve target 'Google Inc.:Google APIs和Open the Java build path property page of project
- uip UDP 服务器广播模式(客户端可以任意端口,并且主动向客户端发送数据) (转)
- Android GUI 单线程消息队列机制 —— 多线程GUI工具箱:一个破碎的梦
- HDOJ 1711 Number Sequence(KMP简单模板题)
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
- String.valueOf() toString() i+"" 效率