liuchuo / PAT

🍭 浙江大学PAT题解(C/C++/Java/Python) - 努力成为萌萌的程序媛~

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PAT-B 1018. 锤子剪刀布 (20)(最简洁巧妙的写法)

1982827148 opened this issue · comments

#include<iostream>
using namespace std;
typedef long long ll;
int n,w1,w2;
int win1[3],win2[3];
int change(char c) {
	if(c=='B') return 0;
	else if(c=='C') return 1;
	else return 2;
}
int main() {
	cin>>n;
	for(int i=0; i<n; i++) {
		char a,b;
		cin>>a>>b;
		int k1=change(a),k2=change(b);//由于后续将按字典序排序输出胜利次数最多的手势,可将B、C、J分别编码下标对应 0 1 2,
		if((k1+1)%3==k2) { //又由于B、C、j循环相克,经分析可巧妙地发现得k1赢k2的条件是 (k1+1)%3==k2,
			w1++;
			win1[k1]++;
		} else if((k2+1)%3==k1) {
			w2++;
			win2[k2]++;
		}
	}
	cout<<w1<<" "<<n-w1-w2<<" "<<w2<<endl;
	cout<<w2<<" "<<n-w1-w2<<" "<<w1<<endl;
	char m[3]= {'B','C','J'};
	int a=0,b=0;
	for(int i=1; i<3; i++) {
		if(win1[i]>win1[a])a=i;
		if(win2[i]>win2[b]) b=i;
	}
	cout<<m[a]<<" "<<m[b];
	return 0;
}