您的位置:首页 > 其它

【白书之路】 1585 - Score 统计分数

2015-09-04 15:29 429 查看


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 Tis 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.
The following shows sample input and output for five test cases.



Sample Input

5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX




Sample Input

10
9
7
55
30


计算一个字符串的额分数,使用以下方法计算每一个字符的分数:

    1.如果当前字符是‘O',那么加一分,并且进行步骤二,如果为'X',分数为0

    2.从当前位置向前查找连续的字符,有几个连续的'O',就加几分

最后,将所有字符的分数相加就是这个字符串的分数

#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX 100
using namespace std;

char str[MAX];
int len;
int get_score()
{
int score=0;
int i,j;
for(i=0;i<len;i++)
{
if(str[i]=='O')//如果当前字符是O
{
j=i;
while(j>=0&&str[j]=='O')//向前查找
{
score++;
j--;
}
}
}
return score;
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
len=strlen(str);
printf("%d\n",get_score());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息