1018. 锤子剪刀布 (20)

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:

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

输入格式:

输入第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
//用一个map将包剪锤转换成数字,然后相减,如果小于0就加3,得到的数字就是比赛结果,最后用一个对相统计
//用cout会超时

#include <iostream>
#include <map>
#include <stdio.h>

using namespace std;

map<char, int> name_to_num;
const char num_to_name[] = { 'B', 'C', 'J'};

class Player{
private:
	int times[3] ;
	int games[3];
public:
	Player(){
		for (int i = 0; i < 3; i++){
			times[i] = 0;
			games[i] = 0;
		}
	};
	void win(int action){
		games[0] ++;
		times[action] ++;
	}
	void lose(){
		games[2]++;
	}
	void draw(){
		games[1]++;
	}
	void printRes(){
		for (int i = 0; i < 3; ++i){
			if (i == 2){
				printf("%d\n", games[i]);
				//cout << games[i]<<endl;
			}
			else{
				printf("%d ", games[i]);
				//cout << games[i] << " ";
			}
		}
	}
	char mostTimes(){
		int maxTime = 0;
		int maxPos = 0;
		for (int i = 0; i < 3; ++i){
			if (times[i] > maxTime){
				maxTime = times[i];
				maxPos = i;
			}
		}
		return num_to_name[maxPos];
	}

};


int main(){

	name_to_num.insert(pair<char, int>('B',0));
	name_to_num.insert(pair<char, int>('C', 1));
	name_to_num.insert(pair<char, int>('J', 2));
	Player playerA, playerB;

	int n; cin >> n;
	char a, b;
	while (n--){
		//scanf("%c %c", &a, &b);
		cin >> a >> b;
		int tmp = name_to_num[a] - name_to_num[b];
		if (tmp < 0){
			tmp += 3;
		}
		if (tmp == 0){
			playerA.draw();
			playerB.draw();
		}
		else if (tmp == 1){
			playerA.lose();
			playerB.win(name_to_num[b]);
		}
		else if (tmp == 2){
			playerA.win(name_to_num[a]);
			playerB.lose();
		}
	}

	playerA.printRes();
	playerB.printRes();
	printf("%c %c", playerA.mostTimes(), playerB.mostTimes());
	//cout << playerA.mostTimes() << " " << playerB.mostTimes()<<endl;

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值