1018 锤子剪刀布 (20分)
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第 1 行给出正整数 N(≤),即双方交锋的次数。随后 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> #include<string> using namespace std; int main() { int n; string str1 = "C"; string str2 = "J"; string str3 = "B"; while (cin >> n) { int sucnt = 0, falcnt = 0, faircnt = 0; int sucnt1 = 0, sucnt2 = 0, sucnt3 = 0; int falcnt1 = 0, falcnt2 = 0, falcnt3 = 0; string s1, s2; while (n--) { cin >> s1 >> s2; if (s1 == str1)//当出锤子时,对方出的3中情况 { if (s2 == str1)//对方出锤子 faircnt++;//平数加1; else if (s2 == str2)//对方出剪刀 sucnt1++;//出锤子的胜数+1 else if (s2 == str3)//对方出布 falcnt3++;//对方出布的胜数加一,即你自己负数加一 } else if (s1 == str2)//出剪刀3种 { if (s2 == str1) falcnt1++; else if (s2 == str2) faircnt++; else if (s2 == str3) sucnt2++; } else if (s1 == str3)//出布3中 { if (s2 == str1) sucnt3++; else if (s2 == str2) falcnt2++; else if (s2 == str3) faircnt++; } }//总胜数=锤子胜数+剪刀胜数+布胜数; sucnt = sucnt1 + sucnt2 + sucnt3; falcnt= falcnt1 + falcnt2 + falcnt3;//总负数=对方胜数=对方锤子胜数+对方剪刀胜数+对方布胜数; cout << sucnt << " " << faircnt << " " << falcnt << endl; cout << falcnt << " " << faircnt << " " << sucnt << endl; int maxs = 0,maxf=0, a[3] = { sucnt1,sucnt2,sucnt3 }, b[3] = { falcnt1 ,falcnt2 ,falcnt3 };//两方的各种胜数放进数组 for (int i = 0; i < 3; i++)//求出最大值, { if (maxs < a[i]) maxs = a[i]; if (maxf < b[i]) maxf = b[i]; } if (maxs == sucnt3)//因为题目要求相等时 按字母序小的输出,所以只要max=sucnt3,就输出B,因为,c j b中b最小 { cout << str3 << ' '; } else if (maxs == sucnt1)//!=sucnt3,但是=sucntl,输出c cout << str1 << ' '; else if (maxs == sucnt2)//输出j cout << str2 << ' '; if (maxf == falcnt3)//同上 { cout << str3 << endl; } else if (maxf == falcnt1) cout << str1 << endl; else if (maxf == falcnt2) cout << str2 << endl; } return 0; }