1018 hammer scissor cloth (20 points) PAT Java code to realize Niu Ke AC

1018 hammer scissors (20 points)

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

Here is the record of the fight between the two. Please count the number of wins, evenings and defeats of both sides, and give the most successful hand gestures of both sides.

Input format:

Enter the first line to give a positive integer N (≤ 105 {^ 5} 5), that is, the number of times the two sides are fighting. Then N lines, each line gives the information of a confrontation, that is, the gestures given by both parties at the same time. C stands for "hammer", J stands for "scissors", B stands for "cloth", the first letter stands for Party A, the second for Party B, and there is a space in the middle.

Output format:

The first and second lines of the output show the win, level and negative times of a and B respectively, and the numbers are separated by a space. The third line gives two letters, representing the gesture with the most winning times of a and B respectively, with a space in the middle. If the solution is not unique, the solution with the smallest alphabetical order is output.

Input example:

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

Output example:

5 3 2
2 3 5
B B

Solutions:

The meaning of this question is easy to understand, but we haven't found a suitable data structure to process the data. The code is a little complicated.

Code implementation:

See code Notes for specific implementation

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        //Record the gesture when A wins
        Map<String,Integer> aWin = new HashMap<>(16);
        //Record the gesture when A wins
        Map<String,Integer> bWin = new HashMap<>(16);
        //Record the number of times A wins and B loses
        int win = 0;
        //Record the number of draws
        int equal = 0;
        //Record the number of times A loses and B wins
        int lose = 0;
        //Read in a blank line first
        sc.nextLine();
        while(number-- !=0){
            char[] chars = sc.nextLine().replace(" ","").toCharArray();
            int check = check(chars[0], chars[1]);
            if(check == 1){
                win++;
                aWin.putIfAbsent(chars[0]+"",0);
                aWin.put(chars[0]+"", aWin.get(chars[0]+"") + 1);
            }else if(check == -1){
                lose++;
                bWin.putIfAbsent(chars[1]+"",0);
                bWin.put(chars[1]+"", bWin.get(chars[1]+"") + 1);
            }else {
                equal++;
            }
        }
        int max = 0;
        String c = "B";
        //Output in format
        System.out.printf("%d %d %d\n",win, equal, lose);
        System.out.printf("%d %d %d\n",lose, equal, win);
		//Output the most winning gestures
        for(String key : aWin.keySet()){
            if(aWin.get(key) > max){
                max = aWin.get(key);
                c = key;
            }
            if(aWin.get(key) == max){
                if(key.compareTo(c) < 0){
                    max = aWin.get(key);
                    c = key;
                }
            }
        }
        System.out.print(c+" ");
        max = 0;
        c = "B";
        for(String key : bWin.keySet()){
            if(bWin.get(key) > max){
                max = bWin.get(key);
                c = key;
            }
            if(bWin.get(key) == max){
                if(key.compareTo(c) < 0){
                    max = bWin.get(key);
                    c = key;
                }
            }
        }
        System.out.print(c);
    }

    //A win return 1 level return 0 A loss return - 1
    public static int check(char a, char b){
        if(a == b){
            return 0;
        }
        if(a == 'C'){
            if(b == 'J'){
                return 1;
            }
            if(b == 'B'){
                return -1;
            }
        }else if(a == 'J'){
            if(b == 'C'){
                return -1;
            }
            if(b == 'B'){
                return 1;
            }
        }else{
            if(b == 'C'){
                return 1;
            }
            if(b == 'J'){
                return -1;
            }
        }
    return 0;
    }
}

Tags: Java

Posted on Thu, 04 Jun 2020 07:22:11 -0700 by jonno946