1018 锤子剪刀布 (20 分)

解题思路
计算甲乙两者的胜负,相当于零和博弈,甲赢了乙肯定输了,所以记录甲就行了,乙的胜负与之相反。
注意
最后需要输出获胜次数最多的手势,我开始是用数组记录,将获胜的手势字符转变为ASCII对应的数值存在数组中,然后从数组中找出最大值,最后用char输出该结果,但没通过。我觉得逻辑上没问题,后来我实在没办法了,只能搞了个map通过了。


#include<cstdio>
#include<map>
using namespace std;
int check(char a,char b){
	if(a==b) 
		return 0;
	else if(a=='J'&&b=='B')
		return 1;
	else if(a=='B'&&b=='C')
		return 1;
	else if(a=='C'&&b=='J')
		return 1;
	else 
		return -1;
}
int main(){
	int n;
	char ch1,ch2;
	map<char,int> mpA,mpB;
	mpA.clear();
	mpB.clear();
	int  win=0,even=0,lose=0;  
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		getchar(); //这个地方是为了防止后面接收字符时把上一次的换行也读入进来了
		scanf("%c %c",&ch1,&ch2);
		if(check(ch1,ch2)==1){
			win++;
			mpA[ch1]++;
		}else if(check(ch1,ch2)==0){
			even++;
		}else{
			lose++;
			mpB[ch2]++;
		}
	}
	printf("%d %d %d\n",win,even,lose);
	printf("%d %d %d\n",lose,even,win); 
	map<char,int>::iterator it;
	int max=0;
	for(it=mpA.begin();it!=mpA.end();it++){
		if(it->second > max){
			ch1=it->first;
			max=it->second;
		}
	}
	max=0;
	for(it=mpB.begin();it!=mpB.end();it++){
		if(it->second > max){
			ch2=it->first;
			max=it->second;
		}
	}
	printf("%c %c\n",ch1,ch2);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值