题目相关
题目链接
洛谷,https://www.luogu.com.cn/problem/P1042。
我的OJ,http://47.110.135.197/problem.php?id=4746。
题目描述
华华通过以下方式进行分析,首先将比赛每个球的胜负列成一张表,然后分别计算在 11 分制和 21 分制下,双方的比赛结果(截至记录末尾)。
比如现在有这么一份记录,(其中W表示华华获得一分,L表示华华对手获得一分):
WWWWWWWWWWWWWWWWWWWWWWLW
在 11 分制下,此时比赛的结果是华华第一局 11 比 0 获胜,第二局 11 比 0 获胜,正在进行第三局,当前比分 1 比 1。而在 21 分制下,此时比赛结果是华华第一局 21 比 0 获胜,正在进行第二局,比分 2 比 1。如果一局比赛刚开始,则此时比分为 0 比 0。直到分差大于或者等于 2,才一局结束。
你的程序就是要对于一系列比赛信息的输入(WL形式),输出正确的结果。
输入格式
每个输入文件包含若干行字符串,字符串有大写的 W、L 和 E 组成。其中 E 表示比赛信息结束,程序应该忽略 E 之后的所有内容。
输出格式
输出由两部分组成,每部分有若干行,每一行对应一局比赛的比分(按比赛信息输入顺序)。其中第一部分是 11 分制下的结果,第二部分是 21 分制下的结果,两部分之间由一个空行分隔。
输入样例
WWWWWWWWWWWWWWWWWWWW
WWLWE
输出样例
11:0
11:0
1:1
21:0
2:1
说明
每行至多 25 个字母,最多有 2500 行。
题目分析
题意分析
这是一道模拟题。根据输入的字符,分析相应的比赛比分。
样例数据分析
根据输入样例,我们列出下面的一个表格,模拟分析整个输入的结果。
11分制比分 | 21分制比分 | |
W | 1:0 | 1:0 |
W | 2:0 | 2:0 |
W | 3:0 | 3:0 |
W | 4:0 | 4:0 |
W | 5:0 | 5:0 |
W | 6:0 | 6:0 |
W | 7:0 | 7:0 |
W | 8:0 | 8:0 |
W | 9:0 | 9:0 |
W | 10:0 | 10:0 |
W | 11:0 | 11:0 |
W | 1:0 | 12:0 |
W | 2:0 | 13:0 |
W | 3:0 | 14:0 |
W | 4:0 | 15:0 |
W | 5:0 | 16:0 |
W | 6:0 | 17:0 |
W | 7:0 | 18:0 |
W | 8:0 | 19:0 |
W | 9:0 | 20:0 |
W | 10:0 | 21:0 |
W | 11:0 | 1:0 |
L | 0:1 | 1:1 |
W | 1:1 | 2:1 |
E |
从上表,我们可以看出比分的变化。当输入 E 后,我们按照要求输出比分。
算法细节
比分如何保存?
我使用了 STL 的 queue 来保存比分。主要是懒得多想。
判断每局结束?
这里有一个小坑,要特别注意要至少净胜 2 分,即分差大于或者等于 2。也就是说,如果是 11 分制,至少一人达到 11 分以上,并且净胜 2 分。21 分制判断方法类似。我使用如下写法:
if ((ans1[0]>=11 || ans1[1]>=11) && abs(ans1[0]-ans1[1])>=2) {
...
}
AC 参考代码
#include <iostream>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
using namespace std;
int main() {
char ch;
int ans1[2]={};//11分制比分, ans1[0]第一个人比分, ans1[1]第二人比分
int ans2[2]={};//21分制比分
queue<string> q1;//11分结果
queue<string> q2;//21分结果
while (cin>>ch) {
if ('E'==ch) {
ostringstream ostr1;
ostr1 << ans1[0] << ":" << ans1[1];
q1.push(ostr1.str());
ostringstream ostr2;
ostr2 << ans2[0] << ":" << ans2[1];
q2.push(ostr2.str());
break;
} else {
if ('W'==ch) {
//Win
ans1[0]++;
ans2[0]++;
} else if ('L'==ch) {
//Los
ans1[1]++;
ans2[1]++;
}
//统计11分制结果
if ((ans1[0]>=11 || ans1[1]>=11) && abs(ans1[0]-ans1[1])>=2) {
ostringstream ostr;
ostr << ans1[0] << ":" << ans1[1];
q1.push(ostr.str());
ans1[0] = 0;
ans1[1] = 0;
}
//统计21分制结果
if ((ans2[0]>=21 || ans2[1]>=21) && abs(ans2[0]-ans2[1])>=2) {
ostringstream ostr;
ostr << ans2[0] << ":" << ans2[1];
q2.push(ostr.str());
ans2[0] = 0;
ans2[1] = 0;
}
}
}
//输出
while (false==q1.empty()) {
cout << q1.front() << endl;
q1.pop();
}
cout << endl;
while (false==q2.empty()) {
cout << q2.front() << endl;
q2.pop();
}
return 0;
}
模拟题,没有什么大的难度,只要根据要求一步一步写出代码就可以,也许有些题目代码有点多,但是没有难度。