题目描述
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入
输入第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 <stdio.h>
#include <iostream>
using namespace std;
int max(int a,int b,int c)//求最大值函数的实现
{
int max = 0;
if(a > max)
max = a;
if(b > max)
max = b;
if(c > max)
max = c;
return max;
}
int main()
{
int n;
char a,b;
while(scanf("%d",&n)!=EOF)
{
int count1=0,count2=0,count3=0; //count1代表甲胜,count2代表两平,count3代表乙胜
int countC1=0,countB1=0,countJ1=0,countC2=0,countB2=0,countJ2=0;
//1代表甲方胜,2代表乙方胜
for(int i=0; i<n; i++)
{
cin>>a>>b;
if(a=='C'&&b=='J')
{
countC1++;
count1++;
continue;
}
else if(a=='J'&&b=='C')
{
countC2++;
count3++;
continue;
}
else if(a=='B'&&b=='C')
{
countB1++;
count1++;
continue;
}
else if(a=='C'&&b=='B')
{
countB2++;
count3++;
continue;
}
else if(a=='J'&&b=='B')
{
countJ1++;
count1++;
continue;
}
else if(a=='B'&&b=='J')
{
countJ2++;
count3++;
continue;
}
else
{
count2++;
continue;
}
}
printf("%d %d %d\n",count1,count2,(n-count1-count2));
printf("%d %d %d\n",count3,count2,(n-count3-count2));
int x=max(countB1,countC1,countJ1);
if(x==countB1) //if-else是顺序结构,按BCJ顺序排序
printf("B ");
else if(x==countC1)
printf("C ");
else
printf("J ");
int y=max(countB2,countC2,countJ2);
if(y==countB2)
printf("B\n");
else if(y==countC2)
printf("C\n");
else
printf("J\n");
}
return 0;
}