PAT Class B 1018 Hammer, Paper, Scissors (20 points)

 

Everyone should be able to play the "hammer, paper, scissors" game: two people give gestures at the same time, and the winning and losing rules are as shown in the picture:

FigCJB.jpg

Now given the confrontation record between the two, please count the number of wins, draws, and losses for both sides, and give the gestures that the two sides made that have the greatest chance of winning.

Input format:

Input line 1 gives a positive integer N (≤10​5​​), which is the number of confrontations between the two sides. Then there are N lines, each line gives the information of a confrontation, that is, the gestures given by both parties A and B at the same time. C It represents "hammer", J "scissors" and B "cloth". The first letter represents Party A, the second letter represents Party B, and there is a space in the middle.

Output format:

The 1st and 2nd lines of the output give the number of wins, draws and losses of A and B respectively, with 1 space separating the numbers. Line 3 gives two letters, representing the gestures with the most wins for A and B respectively, with a space in between. If the solution is not unique, output the alphabetical smallest solution.

Input example:

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

Output sample:

5 3 2
2 3 5
B B

 

The focus of this question is in the comments. The three situations (cloth hammer, scissors) are represented by three numbers 0, 1, 2. This is the point to pay attention to in this question, which can save a lot of unnecessary if codes. at the same time,

	int a[3]={0},b[3]={0};//记录甲乙出布、剪刀、锤子赢的数量 

Also worth remembering. In this case of recording the number of times a certain situation occurs, the array has a certain advantage (if t1 (if it is 0, B, cloth) wins, it will be +1 on a[t1])

#include<iostream>
using namespace std;

int change(char x){
	if(x=='B') return 0;
	if(x=='C') return 1;
	if(x=='J') return 2;
	else return -1;
}
char rechange(int x){
	if(x==0) return 'B';
	if(x==1) return 'C';
	if(x==2) return 'J';
	else return 'e';
}
int main(){
	int n,i;
	char x,y;
	int t1,t2,max1,max2,f1,f2;
	int aWinCnt,bWinCnt,abEqualCnt;
	int a[3]={0},b[3]={0};//记录甲乙出布、剪刀、锤子赢的数量 
	cin>>n;
	aWinCnt = 0;
	bWinCnt = 0;
	abEqualCnt = 0;
	max1 = -1;
	f1 = -1;
	max2 = -1;
	f2 = -1;
	while(n--){
		cin>>x>>y;
		t1 = change(x);
		t2 = change(y);
		if((t1+1)%3==t2){//找规律…这个方法应该可以用在许多地方,有数种情况的时候都可以用。(类似剪刀石头布的) 
			aWinCnt++;
			a[t1]++;
		}
		else if((t2+1)%3==t1){
			bWinCnt++;
			b[t2]++;
		}
		else{
			abEqualCnt++;
		}
	}
    cout<<aWinCnt<<" "<<abEqualCnt<<" "<<bWinCnt<<endl;
    cout<<bWinCnt<<" "<<abEqualCnt<<" "<<aWinCnt<<endl;
    for(i=0;i<3;i++){
    	if(max1<a[i]){
    		f1 = i;
    		max1 = a[i];
		}
		if(max2<b[i]){
			f2 = i;
			max2 = b[i];
		}
	}
	//cout<<f1<<" "<<f2<<endl;
	cout<<rechange(f1)<<" "<<rechange(f2)<<endl;
	
} 

 

Guess you like

Origin blog.csdn.net/zjy997/article/details/94473463