乒乓球


国际乒联前主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及。

其中 11 分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役。华华就是其中一位,他退役之后走上了乒乓球研究工作,意图弄明白 11 分制和 21 分制对选手的不同影响。在开展他的研究之前,他首先需要对他多年比赛的统计数据进行一些分析,所以需要你的帮忙。

华华通过以下方式进行分析,首先将比赛每个球的胜负列成一张表,然后分别计算在 11 分制和 21 分制下,双方的比赛结果(截至记录末尾)。

比如现在有这么一份记录,(其中 W 表示华华获得一分,L 表示华华对手获得一分):

1
WWWWWWWWWWWWWWWWWWWWWWLW
  • 在 11 分制下,此时比赛的结果是华华第一局 11 比 0 获胜,第二局 11 比 0 获胜,正在进行第三局,当前比分 1 比 1。
  • 而在 21 分制下,此时比赛结果是华华第一局 21 比 0 获胜,正在进行第二局,比分 2 比 1。
  • 如果一局比赛刚开始,则此时比分为 0 比 0。直到分差大于或者等于 2,才一局结束。

你的程序就是要对于一系列比赛信息的输入(WL形式),输出正确的结果。

输入:每个输入文件包含若干行字符串,字符串有大写的 W、L 和 E 组成。其中 E 表示比赛信息结束,程序应该忽略 E 之后的所有内容。

输出:输出由两部分组成,每部分有若干行,每一行对应一局比赛的比分(按比赛信息输入顺序)。其中第一部分是 11 分制下的结果,第二部分是 21 分制下的结果,两部分之间由一个空行分隔。

样例

1
2
WWWWWWWWWWWWWWWWWWWW
WWLWE
1
2
3
4
5
6
11:0
11:0
1:1

21:0
2:1

试解

版本1

代码冗余!且没有数据的保存(输出后立刻丢失)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;

void func11(string str) {
if (str[0] == 'E') cout << "0:0" << endl;
int i = 0;
while (str[i] != 'E') {
int sc1 = 0;
int sc2 = 0;
while (1) {
if (((sc1 >= 11 || sc2 >= 11) && abs(sc1 - sc2) >= 2) || str[i] == 'E') break;
if (str[i] == 'W') sc1++;
else if (str[i] == 'L') sc2++;
else {
continue;
}
i++;
}
cout << sc1 << ":" << sc2 << endl;
}
cout << endl;
}

void func21(string str) {
if (str[0] == 'E') cout << "0:0" <<endl;
int i = 0;
while (str[i] != 'E') {
int sc1 = 0;
int sc2 = 0;
while (1) {
if (((sc1 >= 21 || sc2 >= 21) && abs(sc1 - sc2) >= 2) || str[i] == 'E') break;
if (str[i] == 'W') sc1++;
else if (str[i] == 'L') sc2++;
else {
cout << "input error!" << endl;
break;
}
i++;
}
cout << sc1 << ":" << sc2 << endl;
}
cout << endl;
}

int main() {
string str;
cin >> str;
func11(str);
func21(str);
return 0;
}

版本2

利用二维数组保存比赛数据 在数据处理完成后再进行输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<cmath>
using namespace std;

int s11[10005][2], cnt11;
int s21[5005][2], cnt21;

void func11(string str) {
int i = 0;
while (str[i] != 'E') {
while (1) {
if (((s11[cnt11][0] >= 11 || s11[cnt11][1] >= 11) && abs(s11[cnt11][0] - s11[cnt11][1]) >= 2) || str[i] == 'E') break;
if (str[i] == 'W') s11[cnt11][0]++;
else s11[cnt11][1]++;
i++;
}
cout << s11[cnt11][0] << ":" << s11[cnt11][1] << endl;
cnt11++;
}
cout << endl;
}

void func21(string str) {
int i = 0;
while (str[i] != 'E') {
while (1) {
if (((s21[cnt21][0] >= 21 || s21[cnt21][1] >= 21) && abs(s21[cnt21][0] - s21[cnt21][1]) >= 2) || str[i] == 'E') break;
if (str[i] == 'W') s21[cnt21][0]++;
else s21[cnt21][1]++;
i++;
}
cout << s21[cnt21][0] << ":" << s21[cnt21][1] << endl;
cnt21++;
}
cout << endl;
}

int main() {
string str;
cin >> str;
func11(str);
func21(str);
return 0;
}

版本3

while循环中的逻辑重写(单行输入)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<cmath>
using namespace std;

int s11[10005][2], cnt11;
int s21[5005][2], cnt21;

void print() {
for (int i = 0; i <= cnt11; ++i) {
cout << s11[i][0] << ":" << s11[i][1] << endl;
}
cout << endl;
for (int i = 0; i <= cnt21; ++i) {
cout << s21[i][0] << ":" << s21[i][1] << endl;
}
cout << endl;
}

void func(string str) {
int i = 0;
while (str[i] != 'E') {
while (1) {
if (str[i] == 'E') {
print();
return;
}
if (str[i] == 'W') {
s11[cnt11][0]++;
s21[cnt21][0]++;
} else {
s11[cnt11][1]++;
s21[cnt21][1]++;
}
if ((s11[cnt11][0] >= 11 || s11[cnt11][1] >= 11) && abs(s11[cnt11][0] - s11[cnt11][1]) >= 2) cnt11++;
if ((s21[cnt21][0] >= 21 || s21[cnt21][1] >= 21) && abs(s21[cnt21][0] - s21[cnt21][1]) >= 2) cnt21++;
i++;
}
}
cout << endl;
}

int main() {
string str;
cin >> str;
if (str[0] == 'E') {
print();
} else {
func(str);
}
return 0;
}

版本4

将整行输入 改为多行输入(逻辑重写)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<cmath>
using namespace std;

int s11[10005][2], cnt11;
int s21[5005][2], cnt21;

void print() {
for (int i = 0; i <= cnt11; ++i) {
cout << s11[i][0] << ":" << s11[i][1] << endl;
}
cout << endl;
for (int i = 0; i <= cnt21; ++i) {
cout << s21[i][0] << ":" << s21[i][1] << endl;
}
cout << endl;
}

void func(string str) {
for (int i = 0; str[i]; ++i) {
if (str[i] == 'E') {
print();
return;
}
if (str[i] == 'W') {
s11[cnt11][0]++;
s21[cnt21][0]++;
} else {
s11[cnt11][1]++;
s21[cnt21][1]++;
}
if ((s11[cnt11][0] >= 11 || s11[cnt11][1] >= 11) && abs(s11[cnt11][0] - s11[cnt11][1]) >= 2) cnt11++;
if ((s21[cnt21][0] >= 21 || s21[cnt21][1] >= 21) && abs(s21[cnt21][0] - s21[cnt21][1]) >= 2) cnt21++;
}
}

int main() {
string str;
while (cin >> str) {
func(str);
}
return 0;
}

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<cmath>
using namespace std;

//利用二维数组记录数据
//s11 s12为第(0-10005)/(0-5005)局 第(0-1)个人的分数
int s11[10005][2], cnt11;
int s21[5005][2], cnt21;

void print() {
for (int i = 0; i <= cnt11; ++i) {
cout << s11[i][0] << ":" << s11[i][1] << endl;
}
cout << endl;
for (int i = 0; i <= cnt21; ++i) {
cout << s21[i][0] << ":" << s21[i][1] << endl;
}
cout << endl;
}

int main() {
char s[30];//每一行最多25个字符
while (cin >> s) {
for (int i = 0; s[i]; ++i) {
if (s[i] == 'E') {
print();
return 0;
}
if (s[i] == 'W') {
s11[cnt11][0]++;
s21[cnt21][0]++;
} else {
s11[cnt11][1]++;
s21[cnt21][1]++;
}
//很巧妙的将11分制 与 21分制的情况结合起来 进行处理
if ((s11[cnt11][0] >= 11 || s11[cnt11][1] >= 11) && abs(s11[cnt11][0] - s11[cnt11][1]) >= 2) cnt11++;
if ((s21[cnt21][0] >= 21 || s21[cnt21][1] >= 21) && abs(s21[cnt21][0] - s21[cnt21][1]) >= 2) cnt21++;
}
}
return 0;
}