当前位置: 代码迷 >> 综合 >> 1018.锤子剪刀布
  详细解决方案

1018.锤子剪刀布

热度:23   发布时间:2023-12-22 02:00:22.0

1018.锤子剪刀布

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

输入格式:

输入第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

C++版本一:

#include<iostream>
#include<string>
using namespace std;
/*返回甲方的胜负情况,1为胜,0为负,2为平
甲输就是乙胜*/
int cmp(char a, char b) {if ((a =='C'&&b =='J')||(a=='J'&&b=='B')||(a=='B'&&b=='C'))return 1;if (a == b)return 2;elsereturn 0;
}
int main() {int n=0;char a, b;int count_s = 0,count_p=0,count_f=0;//甲方胜的次数,平局次数,输次数int count_c = 0, count_j = 0, count_b = 0;int count_c0 = 0, count_j0 = 0, count_b0 = 0;char A, B;//记录胜出多的手势int count_temp;cin>>n;while (n--) {cin >> a >> b;if (cmp(a, b) == 1) {count_s++;if (a == 'C') count_c++;if (a == 'J') count_j++;if (a == 'B') count_b++;}if (cmp(a, b) == 2) {count_p++;}if (cmp(a, b) == 0) {count_f++;if (b == 'C') count_c0++;if (b == 'J') count_j0++;if (b == 'B') count_b0++;}}A = count_c >= count_j ? 'C' : 'J';count_temp = count_c >= count_j ? count_c : count_j;if (count_b >= count_temp) {A = 'B';}B = count_c0 >= count_j ? 'C' : 'J';//若相等则取序列小的字母count_temp = count_c0 >= count_j0 ? count_c0 : count_j0;if (count_b0 >= count_temp) {B = 'B';}cout << count_s <<" "<< count_p <<" "<< count_f << endl;cout << count_f <<" "<< count_p <<" "<< count_s << endl;cout << A << " " <<B<< endl;return 0;
}


  相关解决方案