您的位置:首页 > 其它

UVa 1585 Score

2016-05-21 09:18 239 查看
本题是较为简单的字符串匹配问题,在匹配过程中遇到’O’则累加,遇到’X’则重置0,以下是代码部分

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

char str[81];  //输入的目标匹配串
int  score[81];  //累加计数变量

int main()
{
int n;
scanf("%d",&n);
while (n --) {
scanf("%s",str);
score[0] = (str[0] == 'O');
for (int i = 1 ; str[i] ; ++ i) {  //str[i]=='\0'时退出循环
score[i] = score[i-1]+1;  //变量累加
if (str[i] == 'X')
score[i] = 0;
}
int sum = 0;
for (int i = 0 ; str[i] ; ++ i)
sum += score[i];

printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: