/*
很水的一道题目,但就是提醒大家注意数组不要越界
我用vs来敲的代码,vs用神奇的优化功能帮我屏蔽了错误
但是UVa不允许,越界在UVa中直接会输出WA
*/
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
char s[110];
int T;
scanf("%d", &T);
while (T--)
{
int sum = 0;
scanf("%s", s);
for (int i = 0; ; i++)
{//起初我在这里的判断条件写上s[i]想在循环到达末尾的时候跳出去,后来发现不仅没必要,而且按照这个思路写还会WA
int count = 0;
//if (s[i] == 'O')
while (s[i] == 'O')
{//统计O出现的次数
count++;
i++;
}
sum += ((1 + count)*count) / 2;//等差数列求和
if (!s[i])//当到达末尾时必须及时跳出循环,否则再这之后还有一次i++导致数组越界
break;//我第一次WA的时候就是在这里
}
printf("%d\n", sum);
}
//system("pause");
return 0;
}
UVA - 1585 - Score
最新推荐文章于 2020-03-06 13:39:42 发布