这个题的测试用例有点怪,所以要小小的处理一下,否则一个用例也通不过。借鉴以为大佬的思路并改正如下:


public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String[] str = in.nextLine().split("\\s+");
            for (int i = 0; i < str.length; i = i + 2) {
                String str1 = str[i];
                String str2 = str[i + 1];
                int[] num1 = charToInt(str1);
                int[] num2 = charToInt(str2);
                Arrays.sort(num1);
                Arrays.sort(num2);
                int grades1 = getGrades(num1);
                int grades2 = getGrades(num2);
                int res = 0 ;
                if (grades1 > grades2) {
                    res = 1;
                } else if (grades1 < grades2) {
                    res = -1;
                } else {
                    res = 0;
                }
                System.out.print(res + " ");
            }
            System.out.println();
        }
    }

    public static int getGrades(int[] arr) {
        if (arr[0] == arr[1] && arr[1] == arr[2]) {
            return arr[2] * 100000;
        } else if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) {
            return arr[2] * 10000;
        } else if (arr[0] == arr[1]) {
            return arr[0] * 1000 + arr[2];
        } else if (arr[1] == arr[2]) {
            return arr[1] * 1000 + arr[0];
        } else {
            return arr[2] * 100 + arr[1] * 10 + arr[0];
        }
    }

    public static int[] charToInt(String str) {
        int[] arr = new int[3];
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            switch (str.charAt(i)) {
                case 'J':
                    arr[count++] = 11;
                    break;
                case 'Q':
                    arr[count++] = 12;
                    break;
                case 'K':
                    arr[count++] = 13;
                    break;
                case '1':
                    arr[count++] = 10;
                    break;
                case '0':
                    break;
                default:
                    arr[count++] = str.charAt(i) - '0';
                    break;
            }
        }
        return arr;
    }
}