大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第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>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
using namespace std;
int main()
{
int N = 0, i = 0;
map<char,int> map1, map2;
int res1[3] = {0}, res2[3] = {0};//0 赢 1平局 2输
char ch1 = 0, ch2 = 0, maxch = 0;
int maxx = 0;
map<char,int>::iterator it;
/* 读取数据,并进行处理 */
cin >> N;
for(i = 0; i < N; i++)
{
cin >> ch1 >> ch2;
if (ch1 == ch2) //平手
{
res1[1]++;
res2[1]++;
}
else if ((ch1 == 'C' && ch2 == 'J') //甲赢
|| (ch1 == 'J' && ch2 == 'B')
|| (ch1 == 'B' && ch2 == 'C'))
{
res1[0]++;
res2[2]++;
map1[ch1]++;
}
else
{
res1[2]++;
res2[0]++;
map2[ch2]++;
}
}
/* 输出数据 */
cout << res1[0] << " " << res1[1] << " " << res1[2] << endl;
cout << res2[0] << " " << res2[1] << " " << res2[2] << endl;
/* 找到甲获胜次数最多的手势 */
maxch = 'B';
for(it = map1.begin(); it != map1.end(); it++)
{
if (it->second > maxx)
{
maxch = it->first;
maxx = it->second;
}
}
cout << maxch << " ";
/* 找到已获胜次数最多的手势 */
maxch = 'B';
maxx = 0; //防止甲的结果影响乙
for(it = map2.begin(); it != map2.end(); it++)
{
if (it->second > maxx)
{
maxch = it->first;
maxx = it->second;
}
}
cout << maxch << endl;
return 0;
}