PAT Basic 1018 Hammer Scissor Cloth (20 points)

Posted by jumpenjuhosaphat on Wed, 31 Jul 2019 08:22:07 +0200

Everyone should play the game of "Hammer Scissors Cloth". Both of them give gestures at the same time. The winning and losing rules are shown in the figure.

Given the record of the confrontation between two people, please count the number of wins, equals and loses of both sides, and give what gestures the two sides make to win the most.

Input format:
Input line 1 gives a positive integer N (< 10)
​5
The number of confrontations between the two sides. Then, in line N, each line gives the information of a confrontation, that is, the gestures given by both sides at the same time. C stands for hammer, J for scissors, B for cloth, the first letter for Party A, the second for Party B, with a space in the middle.

Output format:
Output lines 1 and 2 give the number of wins, equals and loses for A and B, separated by a space. Line 3 gives two letters, representing the gestures with the largest number of wins for A and B, with one space in the middle. If the solution is not unique, the output will be the smallest solution in alphabetical order.

Input sample:
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
#include <iostream>
using namespace std;
bool judgeASuc(char ch1,char ch2){//Judging whether to lose or win
    if(ch1=='C'&&ch2=='J') return true;
    if(ch1=='J'&&ch2=='B') return true;
    if(ch1=='B'&&ch2=='C') return true;
    return false;
}
char max(int CNum,int JNum,int BNum){//Pay attention to alphabetical order with minimum priority,Judgment conditions cannot be changed
    if(BNum>=CNum&&BNum>=JNum) return 'B';
    if(CNum>=BNum&&CNum>=JNum) return 'C';
    if(JNum>=CNum&&JNum>=BNum) return 'J';
}
int main()
{
    int N;char ch1,ch2;
    cin>>N;
    int ASuc=0,AFail=0,ABa=0,BSuc=0,BFail=0,BBa=0;//Number of wins and losses
    int C=0,J=0,B=0,C2=0,J2=0,B2=0;//Number of strikes
    for(int i=0;i<N;i++){
        cin>>ch1>>ch2;
        if(ch1==ch2) {
            ABa++;BBa++;continue;
        }
        if(judgeASuc(ch1,ch2)){
            switch(ch1){
            case 'C':C++;break;
            case 'J':J++;break;
            case 'B':B++;break;
            }
            ASuc++;BFail++;
        }else{
            switch(ch2){
            case 'C':C2++;break;
            case 'J':J2++;break;
            case 'B':B2++;break;
            }
            AFail++;BSuc++;
        }//Comparisons within the loop
    }
    //output
    cout<<ASuc<<" "<<ABa<<" "<<AFail<<endl;
    cout<<BSuc<<" "<<BBa<<" "<<BFail<<endl;
    cout<<max(C,J,B)<<" "<<max(C2,J2,B2);
    system("pause");
    return 0;
}

Note: The workload is relatively large, and it is easy to make logical mistakes.

Topics: PHP