1018. 锤子剪刀布 (20)

题目描述

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:



现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入格式:

输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。

输出格式:

输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯一,则输出按字母序最小的解。

输入样例:

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

输出样例:

5 3 2
2 3 5
B B

提交代码

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
#include <cstdio>
#include <map>
struct player
{
std::map<char, int> cjb;
int win = 0;
int draw = 0;
int loss = 0;
};

int main()
{
int n;
char charA, charB, max;
player playerA, playerB;

scanf("%d", &n);
getchar();
while (n--) {
scanf("%c %c", &charA, &charB);
getchar();
if (charA == charB)
playerA.draw++;
else if ((charA == 'C' && charB == 'J') ||
(charA == 'J' && charB == 'B') ||
(charA == 'B' && charB == 'C')) {
playerA.win++;
playerA.cjb[charA]++;
} else if ((charB == 'C' && charA == 'J') ||
(charB == 'J' && charA == 'B') ||
(charB == 'B' && charA == 'C')) {
playerA.loss++;
playerB.cjb[charB]++;
}
}
printf("%d %d %d\n", playerA.win, playerA.draw, playerA.loss);
printf("%d %d %d\n", playerA.loss, playerA.draw, playerA.win);
max = playerA.cjb['C'] >= playerA.cjb['J'] ? 'C' : 'J';
max = playerA.cjb[max] > playerA.cjb['B'] ? max : 'B';
printf("%c ", max);
max = playerB.cjb['C'] >= playerB.cjb['J'] ? 'C' : 'J';
max = playerB.cjb[max] > playerB.cjb['B'] ? max : 'B';
printf("%c\n", max);

return 0;
}

个人思考

这道题还是挺简单的,主要问题还是在细节,对于scanf()读取字符的时候要注意\n的问题,容易读取错误。