题解 | #扑克牌大小#

扑克牌大小

https://www.nowcoder.com/practice/d290db02bacc4c40965ac31d16b1c3eb

import java.util.Scanner;

public class Main {


1:对每种牌型进行编号
  个0    牌数为1
  对子1  牌数为2
  顺子2  牌数为5
  三张   牌数为3
  四张   牌数为4
  
  其实编号和牌数的顺序保持一致比较好。
  
  
2: 标识矩阵,标识每两种类型的牌是否能进行比较
  	0,无法进行
  	1,同种类型可以进行比较
    2, a < b
    3,  a > b
  
3: 同种类型的牌,只需要比较第一个字符即可,注意joker JOKER单张的情况优先处理一下
  
  
 
  
  
  
  public static void main(String[] args) {

        int[][] flag = new int[6][6];

        flag[0] = new int[] {1, 0, 0, 0, 2, 2};
        flag[1] = new int[] {0, 1, 0, 0, 2, 2};
        flag[2] = new int[] {0, 0, 1, 0, 2, 2};
        flag[3] = new int[] {0, 0, 0, 1, 2, 2};
        flag[4] = new int[] {3, 3, 3, 3, 1, 2};
        flag[5] = new int[] {3, 3, 3, 3, 3, 3};

        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        String item[] = line.split("-");

        if (item[0].equals("joker JOKER") || item[1].equals("joker JOKER")) {
            System.out.println("joker JOKER");
            System.exit(0);
        }

        int typeA = type(item[0]);
        int typeB = type(item[1]);

        int compareFlag = flag[typeA][typeB];
        if (compareFlag == 0) {
            System.out.println("ERROR");
        } else if (compareFlag == 1) {
            compareFirst(item[0], item[1]);
        } else if (compareFlag == 2) {
            System.out.println(item[1]);
        } else {
            System.out.println(item[0]);
        }
    }

    public static void compareFirst(String item1, String item2) {

        int num1 = convert(item1);
        int num2 = convert(item2);

        if (num1 > num2) {
            System.out.println(item1);
        } else {
            System.out.println(item2);
        }
    }


    public static int convert(String item) {

        if (item.equals("JOKER")) {
            return 10000;
        } else if (item.equals("joker")) {
            return 100;
        }

        char ch = item.charAt(0);

        if (ch == '3') {
            return 3;
        } else if (ch == '4') {
            return 4;
        } else if (ch == '5') {
            return 5;
        } else if (ch == '6') {
            return 6;
        } else if (ch == '7') {
            return 7;
        } else if (ch == '8') {
            return 8;
        } else if (ch == '9') {
            return 9;
        } else if (ch == '1') {
            return 10;
        } else if (ch == 'J') {
            return 11;
        } else if (ch == 'Q') {
            return 12;
        } else if (ch == 'K') {
            return 13;
        } else if (ch == 'A') {
            return 14;
        } else if (ch == '2') {
            return 15;
        }

        throw new NullPointerException();
    }


    public static int type(String item) {

        int len = item.split(" ").length;
        if (len == 1) {
            return 0;
        } else if (len == 2) {
            return 1;
        } else if (len == 5) {
            return 2;
        } else if (len == 3) {
            return 3;
        } else if (len == 4) {
            return 4;
        } else {
            throw new NullPointerException();
        }

    }
}

全部评论

相关推荐

头像
03-18 09:09
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务