刘汝佳的算法竞赛入门经典(第2版) 习题解答
来源:互联网 发布:俄罗斯聊天软件下载 编辑:程序博客网 时间:2024/06/05 16:10
3-1.1585
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 previousconsecutive `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.
The following shows sample input and output for five test cases.
Sample Input
5
OOXXOXXOOO
OOXXOOXXOO
OXOXOXOXOXOXOX
OOOOOOOOOO
OOOOXOOOOXOOOOX
Sample Output
10
9
7
55
30
解答如下:
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
int main()
{
int x,y,T,flag=0,sum=0;
vector<vector<char> > str;
string s;char c;
cin>>T;
for(x=0;x<T;++x)
{
str.resize(T);
while((c=getchar())!=EOF && c!='#')
{
str[x].push_back(c);
}
}
for(x=0;x<T;++x)
{
sum=0;flag=0;
for(y=0;y<str[x].size();++y)
{
if(str[x][y]=='o' && flag==0)
{
flag=1;
sum+=1;
}else if(str[x][y]=='o' && flag>0)
{
++flag;
sum+=flag;
}
if(str[x][y]=='x')
{
flag=0;
sum+=0;
}
}
cout<<sum<<endl;
}
}
输出结果截图为:
0 0
- 刘汝佳的算法竞赛入门经典(第2版) 习题解答
- 算法竞赛入门经典 第1章 习题解答
- 算法竞赛入门经典第一章习题解答
- 算法竞赛入门经典习题解答(2)
- 算法竞赛入门经典(第2版)习题4-8 特别困的学生UVa12108
- 算法竞赛入门经典第2章【小结和习题】
- 算法竞赛入门经典 第2章 习题答案
- 《算法竞赛入门经典》第2章习题总结
- [刷题]算法竞赛入门经典 第2章习题
- 算法竞赛入门经典第二章习题解答
- 算法竞赛入门经典第三章习题解答
- 算法竞赛入门经典第二章习题解答
- 算法竞赛入门经典习题解答(1)
- 算法竞赛入门经典第四章:部分习题解答
- 初学acmer--《算法竞赛经典入门》第二章2.5 习题自己的解答
- 算法竞赛入门经典(第二版)习题解答参考(一)
- 算法竞赛入门经典(第二版)习题解答参考(第二章)
- 算法竞赛入门经典第三章总结(2):后半部分习题解答
- 使用canvas实现行走的小人动画
- 25. Reverse Nodes in k-Group
- git 创建服务端库Shell脚本
- 文章标题
- Android SharePreferences 源码分析
- 刘汝佳的算法竞赛入门经典(第2版) 习题解答
- #循环中的continue 和 break
- zoj2099
- java反射机制
- 简单的js验证码
- 关于maven本地仓库新建项目报错的解决
- 想要学习Linux技术,先好好的读一本Linux书籍吧
- HDU1059:Dividing(多重背包二进制优化)
- lightOJ 1045 Digits of Factorial (数位计数)