【PAT】1018 锤子剪刀布 (20 分)

版权声明:版权所有,转载请注明出处。 https://blog.csdn.net/totalo/article/details/85630856

1018 锤子剪刀布 (20 分)

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:

FigCJB.jpg

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入格式:

输入第 1 行给出正整数 N(≤10​5​​),即双方交锋的次数。随后 N 行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C 代表“锤子”、J 代表“剪刀”、B 代表“布”,第 1 个字母代表甲方,第 2 个代表乙方,中间有 1 个空格。

输出格式:

输出第 1、2 行分别给出甲、乙的胜、平、负次数,数字间以 1 个空格分隔。第 3 行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有 1 个空格。如果解不唯一,则输出按字母序最小的解。

输入样例:

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

输出样例:

5 3 2
2 3 5
B B
import java.util.Scanner;
 
public class Main{
    public static int Judge(String A,String B){
        if(A.charAt(0) == B.charAt(0))
            return 0;
         
        switch(A.charAt(0) - B.charAt(0)){
            case 1:
                return 2;
             
            case -1:
                return 1;
                 
            case 7:
                return 2;
            case -7:
                return 1;
                 
            case 8:
                return 1;
                 
            case -8:
                return 2;
                 
        }
         
        return 1;
    }
     
    public static void main(String [] args){
        Scanner cin = new Scanner(System.in);
        int N;
        String A,B;
        N = cin.nextInt();
        int [] resA = new int[3];
        int [] resB = new int[3];
        int [] winGestureCntA = new int[3];
        int [] winGestureCntB = new int[3];
        int tmp;
        int maxA = 0,maxB = 0;
        int indexA = 0,indexB = 0;
        for(int i = 0;i < N;i ++){
            A = cin.next();
            B = cin.next();
            tmp = Judge(A,B);
            resA[tmp] ++;
             
            if(tmp == 1){
                switch(A.charAt(0)){
                    case 'B':
                        winGestureCntA[0] ++;
                        break;
                         
                    case 'C':
                        winGestureCntA[1] ++;
                        break;
                         
                    case 'J':
                        winGestureCntA[2] ++;
                        break;
                }
            }
             
            else if(tmp == 2){
                switch(B.charAt(0)){
                    case 'B':
                        winGestureCntB[0] ++;
                        break;
                         
                    case 'C':
                        winGestureCntB[1] ++;
                        break;
                         
                    case 'J':
                        winGestureCntB[2] ++;
                        break;
                }
            }
        }
         
        resB[0] = resA[0];
        resB[1] = resA[2];
        resB[2] = resA[1];
        for(int i = 0;i < 3;i ++){
            if(winGestureCntB[i] > maxB){
                maxB = winGestureCntB[i];
                indexB = i;
            }
             
            if(winGestureCntA[i] > maxA){
                maxA = winGestureCntA[i];
                indexA = i;
            }
        }
         
        System.out.println(resA[1] + " " + resA[0] + " " + resA[2]);
        System.out.println(resB[1] + " " + resB[0] + " " + resB[2]);
        switch(indexA){
            case 0:
                System.out.print("B ");
                break;
                 
            case 1:
                System.out.print("C ");
                break;
                 
            case 2:
                System.out.print("J ");
                break;
        }
         
        switch(indexB){
            case 0:
                System.out.print("B");
                break;
                 
            case 1:
                System.out.print("C");
                break;
                 
            case 2:
                System.out.print("J");
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/totalo/article/details/85630856
今日推荐