本文共 1898 字,大约阅读时间需要 6 分钟。
【UVA】 1585 --- Score
There is an objective test result such as “OOXXOXXOOO”. An ‘O’ means a correct answer of a problem
and an ‘X’ means a wrong answer. The score of each problem of this test is calculated by itself and
its just previous consecutive ‘O’s only when the answer is correct. For example, the score of the 10th
problem is 3 that is obtained by itself and its two previous consecutive ‘O’s.
Therefore, the score of “OOXXOXXOOO” is 10 which is calculated by “1+2+0+0+1+0+0+1+2+3”.
You are to write a program calculating the scores of test results.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test
cases T is given in the first line of the input. Each test case starts with a line containing a string
composed by ‘O’ and ‘X’ and the length of the string is more than 0 and less than 80. There is no spaces
between ‘O’ and ‘X’.
Output
Your program is to write to standard output. Print exactly one line for each test case. The line is to
contain the score of the test case.
Sample Input
5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX
Sample Output
10
9
7
55
30
题意:
如何计算你们的得分呢?,如“OOXXOXXOOO”。 “O”表示问题的正确答案,“X”表示错误的答案。那么它得分是由它自己和它刚刚以前连续的’O’只有当答案是正确的。
例如,第10个问题的分数是由其自身和它的两个先前连续的“0”获得的3。
因此,“OOXXOXXOOO”的得分是通过“1 + 2 + 0 + 0 + 1 + 0 + 0 + 1 + 2 + 3”计算的10。你要编写一个计算测试结果分数的程序。
输入
输入第一行一个整数T,表示由T个测试用例组成。
每个测试用例以包含由’O’和’X’组成的字符串的行开始,并且字符串的长度大于0且小于80.在’O’和’X’之间没有空格。
输出
每个测试用例输出一行。该行是包含测试用例的分数。
样例输出
5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX
样例输出
10
9
7
55
30
#include #include using namespace std;int main() { int t; cin >> t; while (t--) { char arr[81]; cin >> arr; int len = strlen(arr); int x = 1, sum = 0; for (int i = 0; i < len; i++) { if (arr[i] == 'O') { sum += x; x++; } else { x = 1; } } cout << sum << endl; } return 0;}
转载地址:http://rgyof.baihongyu.com/