sicily 1388. Quicksum

Description

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.
 
输出是每个字母的号码(A~Z分别为1~26,空格为0)* 该字母位置之和。利用char类型的存储特性很容易写出来。
需要注意的是数组的长度要比需要的大一点,不然会报RF(虽然很奇怪,不过提示说可能和RTE是一样的),第一次设成255的时候就报了,再改成256就AC,应该是数组溢出?
 
发现自己养成了一个习惯,标记控制的部分用while,计数器控制的部分用for:
前者不需要步长,用for的话头会出现空语句不好看;后者把控制条件全写在头里,比while更加直观。
else里的语句其实可以是空的,不过写出来可能也比较好看一点,但是懒得再乘i+1了(咦) 给自己看就好
 
View Code
 1 #include<stdio.h>
 2 int main()
 3 {
 4     int i, total = 0;
 5     char letter[256];
 6     
 7     while ( scanf("%c", &letter[0] )&& letter[0] != '#' )
 8     {
 9         total = total + letter[0] - 64;
10         
11         for ( i = 1; scanf("%c", &letter[i]) && letter[i] != '\n'; i++ )
12         {
13             if ( letter[i] != ' ' )
14             {
15                 total = total + ( letter[i] - 64 ) * ( i + 1 );
16             }
17             else
18             {
19                 total = total + 0;
20             }
21         }
22         
23         printf("%d\n", total);
24         
25         total = 0;
26         
27     }
28     
29     return 0;
30 }
posted @ 2012-11-11 12:52  Joyee  阅读(295)  评论(0编辑  收藏  举报