洛谷原题链接:https://www.luogu.com.cn/problem/P1042
输入
WWWWWWWWWWWWWWWWWWWW
WWLWE
输出
11:0
11:0
1:1
21:0
2:1
注意
1、两种输入方式都可以;
2、abs(W - L) >= 2 && (W >= 11 || L >= 11)
是 &&
号,因为其中一人要至少赢11局;
3、W >= 11
不是 W == 11
,因为一定要比分相差大于2才能结束(比如11:10不能结束,还要继续比)。
#include <iostream>
using namespace std;
int main() {
string str, line;
while (getline(cin, line)) {
size_t pos = line.find('E');
if (pos != string::npos) {
str += line.substr(0, pos + 1);
break;
}
str += line;
}
// while (cin >> line) {
// str += line;
// }
int len = str.size();
int W = 0, L = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'W') W++;
if (str[i] == 'L') L++;
if (abs(W - L) >= 2 && (W >= 11 || L >= 11)) {
cout << W << ":" << L << endl;
W = 0, L = 0;
}
if (str[i] == 'E') {
cout << W << ":" << L << endl;
break;
}
}
W = 0, L = 0;
cout << endl;
for (int i = 0; i < len; i++) {
if (str[i] == 'W') W++;
if (str[i] == 'L') L++;
if (abs(W - L) >= 2 && (W >= 21 || L >= 21)) {
cout << W << ":" << L << endl;
W = 0, L = 0;
}
if (str[i] == 'E') {
cout << W << ":" << L << endl;
break;
}
}
return 0;
}
END -