[PAT B1018] hammer and scissors

We should be able to play "hammer and scissors" game: they both give gesture, as shown in Figure outcome of the rules:

Here Insert Picture Description

Now given the two men clash records, statistics of both wins, flat, negative number, and the two sides are what give the greatest chance of winning gesture.
Input format:
Enter line 1 gives a positive integer N (<= 105), i.e., both the number of confrontation. Then N rows, each row gives information confrontation, i.e., the gesture and B sides at the same time is given. C stands for "hammer", the representative J "scissors", B stands for "cloth", the first letter represents a party, on behalf of the second party, there is an intermediate space.
Output Format:
output the first and second lines are given A, B wins, flat, negative number, separated by an inter-digital space. Line 3 shows two letters represent A, B wins the highest number of gestures, there is an intermediate space. If the solution is not unique, the minimum solution alphabetically output.
Sample input:
10
CJ
JB
CB
BB
the BC
the CC
CB
JB
the BC
JJ
Output Sample:
. 5. 3 2
2. 3. 5
BB

#include <stdio.h>

int num(char c) {
    if (c == 'B') return 0;
    if (c == 'C') return 1;
    if (c == 'J') return 2;
}

int main() {
    int maxa = 0, maxb = 0;
    char mp[3] = {'B', 'C', 'J'};
    int timeA[3] = {0}, timeB[3] = {0}, winHandA[3], winHandB[3]; //time存放 胜 负 平 的次数
    char a, b;
    int numA = 0, numB = 0, N;


    scanf("%d", &N);

    for (int i = 0; i < N; i++) {
        getchar();//吸收空格
        scanf("%c %c", &a, &b);
        numA = num(a);
        numB = num(b);
        //取胜顺序 0--> 1 --> 2 --> 0

        if ((numA + 1) % 3 == numB) {
            //a胜
            timeA[0]++;
            timeB[1]++;
            winHandA[numA]++;
        } else if (numA == numB) {
            //平
            timeA[2]++;
            timeB[2]++;
        } else if ((numB + 1) % 3 == numA) {
            //b胜
            timeA[1]++;
            timeB[0]++;
            winHandB[numB]++;
        }

    }
    for (int i = 0; i < 3; i++) {
        if (winHandA[i] > winHandA[maxa]) //比较字典序
            maxa = i;
        if (winHandB[i] > winHandB[maxb])
            maxb = i;
    }
    printf("%d %d %d\n", timeA[0], timeA[2], timeA[1]);
    printf("%d %d %d\n", timeB[0], timeB[2], timeB[1]);
    printf("%c %c", mp[maxa], mp[maxb]);
}
/*
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
 */

Test Results:
Here Insert Picture Description

Published 33 original articles · won praise 1 · views 4144

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103860086