【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

分析:

大体意思是说给一串由O和X组成的串,计算得分。O代表的得分为当前连续出现O的个数,X为0分。 

这个题比较简单,只需要记录O连续出现的个数,一遇到O 个数+1,遇到X不进行操作。

代码:

最开始我的代码,比较复杂。纠结着用不用数组存放得分串,最终因不想处理“\n”的问题,用了数组,反而把代码变复杂了不少。因为一次进行多组计分,得分串的长度不固定(我本以为,一个数组多次利用,不便于用strlen()。但实际上获取输入内容后,其末尾有'\0',而strlen正是获取'\0'位置,所以此处可以使用strlen函数,不必要这么复杂。)我的方法是在获取得分串前,把数组内元素全部赋值为‘1’,然后通过循环找到第一个‘1’出现的位置,从而判断长度。

判断O是否连续,我采用的是判断前一个和当前一个是否为O (这样又麻烦了不少Orz,可以直接判断O的个数如果>0,而当前又为O,从而判断连续)。

我用的是C,使用数组的代码如下:

#include <stdio.h>
#include <string.h>
char s[90];

int main()
{
	int T, len, i, j, ans;
	scanf("%d",&T);
	memset(s,'1',sizeof(s));
	while (T--)
	{
		scanf("%s",s);
		j=0;//O出现且连续的个数
		ans=0;//得分
		for (i=0; i < 90; i++)//求长度
		{
			if (s[i] == '1')break;//跳出循环后i++;
		}
		len = i-1;
		for (i=0; i < len; i++)
		{
			if (i>0 && s[i]=='O' && s[i-1]=='O')
			{
				j++;
				ans += j;
			}
			else if (s[i] == 'O')
			{
				j=1;
				ans += j;
			}
		}
		memset(s,1,len);
		printf("%d\n",ans);
	}
	return 0;
}

 后来发现不使用数组反而更简单。使用getchar()。判断是不是\n,如果不是再判断是否为‘O’,具体判断连续,可用上面提到的办法,另外,一遇到X,那么O连续的计数(代码中为j)又需从0开始计数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值