问题
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
FigCJB.jpg
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第 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
解决方法
#include<iostream>
using namespace std;
int change(char c)
{
if (c == 'B') return 0;
if (c == 'C') return 1;
if (c == 'J') return 2;
}
int main()
{
int n,flag1=0,flag2=0;
cin >> n;
int jiatimes[3] = { 0 }, yitimes[3] = { 0 },jiahand[3] = { 0 }, yihand[3] = { 0 };
char mp[3] = { 'B','C','J' };
for (int i = 0; i < n; i++)
{
char a, b;
cin >> a >> b;
if ((change(a)+1) % 3 == (change(b)))
{
jiatimes[0]++;
yitimes[2]++;
jiahand[change(a)]++;
}
else if (change(a) == change(b))
{
jiatimes[1]++;
yitimes[1]++;
}
else
{
yitimes[0]++;
jiatimes[2]++;
yihand[change(b)]++;
}
}
cout << jiatimes[0] << " " << jiatimes[1] << " " << jiatimes[2] << endl;
cout << yitimes[0] << " " << yitimes[1] << " " << yitimes[2] << endl;
for (int i = 0; i < 3; i++)
{
if (jiahand[i] > jiahand[flag1]) flag1 = i;
if (yihand[i] > yihand[flag2]) flag2 = i;
}
cout << mp[flag1] << " " << mp[flag2]<<endl;
return 0;
}
后记
在一些地方犯过错:
♦change(a)+1把+1放进了括号里面去了.
♦后来在输出赢的最多的手势时,flag的实时更新出现了问题.
jiahand[i] > jiahand[flag1]//是和flag1进行比较,不是和jiahand[0]比较.
一定要注意a!!!