Requirement:
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 |
Analysis:
关键问题是读取方式,本题中空格也占位,因此要选择不会忽略空格的输入方式。
cin:会忽略回车、空格、tab;
cin.get(char ch):一个一个读,不会忽略任何字符,需要对回车进行特殊处理;
cin.getline(char ch, int n):向ch读入n个字符,包括‘\0’。一行一行读,不会忽略任何字符。
解法一:使用cin.get(char ch)
#include <iostream>
using namespace std;
int main()
{
char ch;
int i=1;
int sum=0;
while(cin.get(ch))
{
if(ch=='#') break;
if(ch!='\n')
{
if(ch!=' ') sum=sum+i*(ch-64);
i++;
}
else
{
cout<<sum<<endl;
sum=0;
i=1;
}
}
return 0;
}
解法二:使用cin.getline(char ch,int n)
#include <iostream>
using namespace std;
int main()
{
char ch[256];
int sum=0;
while(cin.getline(ch,256))
{
if(ch[0]=='#') break;
for(int i=0;ch[i]!='\0';i++)
{
if(ch[i]!=' ') sum=sum+(i+1)*(ch[i]-64);
}
cout<<sum<<endl;
sum=0;
}
return 0;
}