PAT Grade B 1018 Hammer, Scissors, Cloth (20 points)

topic content

Everyone should play the game of "hammer, scissors and paper": two people give gestures at the same time, and the rules of winning and losing are shown in the figure:

FigCJB.jpg

Now give the record of the confrontation between the two, please count the number of wins, draws, and losses of the two sides, and give the gestures of the two sides that have the best chance of winning.

Input format:

Enter line 1 to give a positive integer N (≤105), the number of times the two sides fought. Then there are N lines, each line gives the information of a confrontation, that is, the gestures given by both parties at the same time. C On behalf of "hammer", J on behalf of "scissors", B on behalf of "cloth", the first letter represents Party A, the second represents Party B, 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, and the numbers are separated by a space. Line 3 gives two letters, representing the gestures with the most wins by A and B, with a space in between. If the solutions are not unique, the alphabetically smallest solution is output.

Input sample:

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

no blank line at the end

Sample output:

5 3 2
2 3 5
B B

no blank line at the end

Problem solving ideas

Simulate, use an array to access the gestures of A and B, and use a structure to record the results of each game. The key in the structure represents the letter gesture, and the value records the number of wins for each gesture of A and B. Finally, a cmp function is customized to ensure that the winning For gestures with the same number of fields, the one with the smallest lexicographical order can be output.

Detailed code

#include <iostream>
#include <algorithm>
using namespace std;
char a[100001][2];
int j1,j2,j3,y1,y2,y3;
int jc,jj,jb,yc,yj,yb;
typedef struct{
    char key;
    int value;
}tet;

void judge(char a,char b){
    if(a==b){
        j2++;y2++;
    }
    else if(a == 'C'&&b == 'J'){
        j1++;y3++;jc++;
    }
    else if(a == 'C'&&b == 'B'){
        y1++;j3++;yb++;
    }
    else if(a == 'J'&&b == 'C'){
         y1++;j3++;yc++;
    }
    else if(a == 'J'&&b == 'B'){
        j1++;y3++;jj++;
    }
    else if(a == 'B'&&b == 'C'){
        j1++;y3++;jb++;
    }
    else if(a == 'B'&&b == 'J'){
        y1++;j3++;yj++;
    }
    
    
}

bool cmp(tet a,tet b){
    if(a.value!=b.value){
        return a.value>b.value;
    }
    else return a.key<b.key;
}

char fun(int c,int j,int b){
    tet a[3]={
   
   {'C',c},{'J',j},{'B',b}};
    sort(a,a+3,cmp);
    return a[0].key;
}
int main(){
    int N;
    cin>>N;
    for(int i = 0;i<N;i++){
        cin>>a[i][0]>>a[i][1];
        judge(a[i][0],a[i][1]);
    }
    cout<<j1<<" "<<j2<<" "<<j3<<endl;
    cout<<y1<<" "<<y2<<" "<<y3<<endl;
    cout<<fun(jc,jj,jb)<<" "<<fun(yc,yj,yb);
}

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/119298650