您的位置:首页 > 产品设计 > UI/UE

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;
}


运行结果:

Accepted273415MS1932K515B
题解:

这道题不算很难。易错点在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,美国标准信息交换代码)——》百度


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: