TJU Problem 2520 Quicksum

注意:

  for (int i = 1; i <= aaa.length(); i++)

其中是“ i <= ",注意等号。

原题:

2520.   Quicksum


Time Limit: 0.5 Seconds   Memory Limit: 65536K
Total Runs: 2964   Accepted Runs: 1970



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

Source: Mid-Central USA
2006

源代码:

 1 #include <iostream>
 2 #include <cctype>
 3 #include <string>
 4 using namespace std;
 5
 6 int main()    {
 7     string aaa;    int sum = 0;
 8     while (getline(cin, aaa) && aaa != "#")    {
 9         for (int i = 1; i <= aaa.length(); i++)    {
10             if (isalpha(aaa[i - 1]))    sum += i * (aaa[i - 1] - ‘A‘ + 1);
11             //cout << "sum["<<i<<"] "<<sum << endl;
12         }
13         cout << sum << endl;
14         sum = 0;
15     }
16     return 0;
17 }
时间: 2024-10-16 17:00:08

TJU Problem 2520 Quicksum的相关文章

TJU Problem 1065 Factorial

注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065.   Factorial Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 6067   Accepted Runs: 2679 The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceiver

TJU Problem 1015 Gridland

最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 1 做这题时,千万不要被那个图给吓着了,其实这题就是道简单的数学题. 2 3 首先看当m或n中有一个为2的情况,显然,只需要算周长就OK了.即(m+n-2)*2,考虑到至少其中一个为2,所以答案为2 *m或2*n,亦即m*n.注意这里保证了其中一个数位偶数. 4 当m,n≥3时,考虑至少其中一个为偶数的情况,显然,这种情况很简单,可以得出,结果为m*

TJU Problem 2857 Digit Sorting

原题: 2857.   Digit Sorting Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3234   Accepted Runs: 1704 Several players play a game. Each player chooses a certain number, writes it down (in decimal notation, without leading zeroes) and sorts t

TJU Problem 2101 Bullseye

注意代码中: result1 << " to " << result2 << ", PLAYER 1 WINS."<< endl; 和 result1 << " to " << result2 << ", PLAYER 1 WINS. "<< endl; 虽然只在WINS后只差一个空格,但会导致PE. 原题: 2101.   Bul

TJU Problem 1100 Pi

注: 1. 对于double计算,一定要小心,必要时把与double计算相关的所有都变成double型. 2. for (int i = 0; i < N; i++)         //N 不可写为N - 1,否则当N为1时无法进行: 原题: 1100.   Pi Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 5683   Accepted Runs: 2317 Professor Robert A. J. Matthews

TJU Problem 2548 Celebrity jeopardy

下次不要被长题目吓到,其实不一定难. 先看输入输出,再揣测题意. 原文: 2548.   Celebrity jeopardy Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 1306   Accepted Runs: 898 It's hard to construct a problem that's so easy that everyone will get it, yet still difficult enough

【水】TJU Problem 1805 Electrical Outlets

没看懂题,直接看了INPUT,原来真的可以猜出来. 原题: 1805.   Electrical Outlets Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 3321   Accepted Runs: 2575 Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating ba

TJU Problem 1644 Reverse Text

注意: int N; cin >> N; cin.ignore(); 同于 int N; scanf("%d\n",&N); 另:关于 cin 与 scanf: scanf是格式化输入,printf是格式化输出. cin是输入流,cout是输出流.效率稍低,但书写简便. 格式化输出效率比较高,但是写代码麻烦. 流输出操作效率稍低,但书写简便. cout之所以效率低,正如一楼所说,是先把要输出的东西存入缓冲区,再输出,导致效率降低. 缓冲区比较抽象,举个例子吧: 曾经

TJU Problem 1090 City hall

注:对于每一横行的数据读取,一定小心不要用int型,而应该是char型或string型. 原题: 1090.   City hall Time Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 4874   Accepted Runs: 2395 Because of its age, the City Hall has suffered damage to one of its walls. A matrix with M rows an