[Huawei Machine Test] Select the largest poker card type

topic description

Given a set of playing cards, there are 5 cards in total. Each card has four suits, which are hearts, spades, red squares, and black twists. The value of each card is in the range of [A, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K]. Now given a set of judgment logic, find out its maximum card type.

  • Card Type 1: If five cards appear consecutively and have the same suit, it is called a straight flush . Note that when A is included, only two flush combinations of A, 1, 2, 3, 4 and 10, J, Q, K, A are allowed, and similar combinations of Q, K, A, 1, 2 are not allowed.
  • Card Type 2: If four cards of the same size appear, it is called a quadruple .
  • Card Type Three: If the five cards are of the same suit, it is called a flush .
  • Card type 4: If there are five consecutive cards, it is called a straight [tip: It is required that the suits of the five cards cannot be the same, otherwise it is a straight flush ].
  • Card Type Five: If there are three cards of the same size and two other cards of the same size, it is called a triple .
  • Card Type Six: If three cards of the same size appear and two other cards of a different value, it is called a triple .
  • Card type seven: other combinations.

Now use V to represent hearts, B to represent spades, N to represent diamonds, and M to represent twists. Now enter 5 lines of strings, each line of strings contains two character strings separated by spaces. For example:

Input:
1 N
2 N
3 N
4 N
5 N
Output:
1

It is required to analyze according to the input, and output the maximum card face of the combination [the smaller the card type, the larger the card face], the card face is represented by 1...7 numbers. For example, card type one is represented by 1.


topic analysis

public class Main {
    
    
    public static void main(String[] args) {
    
    
    	p3()
    }
	static void resolve() {
    
    
        Map<String, Integer> map = new HashMap<>();
        map.put("J", 11);
        map.put("Q", 12);
        map.put("K", 13);
        Map<Integer, List<String>> cardMap = new HashMap<>();
        Scanner in = new Scanner(System.in);
        boolean hasA = Boolean.FALSE;
        for (int i = 0; i < 5; ++i) {
    
    
            String[] split = in.nextLine().split(" ");
            String s1 = split[0];
            String s2 = split[1];
            if (s1.equals("A")) {
    
    
                hasA = Boolean.TRUE;
                if (cardMap.containsKey(1) && cardMap.containsKey(14)) {
    
    
                    cardMap.get(1).add(s2);
                    cardMap.get(14).add(s2);
                    continue;
                }
                if (cardMap.containsKey(1) && !cardMap.containsKey(14)) {
    
    
                    cardMap.get(1).add(s2);
                    List<String> list = new ArrayList<>();
                    list.add(s2);
                    cardMap.put(14, list);
                    continue;
                }
                if (!cardMap.containsKey(1) && cardMap.containsKey(14)) {
    
    
                    cardMap.get(14).add(s2);
                    List<String> list = new ArrayList<>();
                    list.add(s2);
                    cardMap.put(1, list);
                    continue;
                }
                List<String> list1 = new ArrayList<>();
                list1.add(s2);
                List<String> list2 = new ArrayList<>();
                list2.add(s2);
                cardMap.put(1, list1);
                cardMap.put(14, list2);
                continue;
            }
            if (map.containsKey(s1)) {
    
    
                Integer key = map.get(s1);
                if (cardMap.containsKey(key)) {
    
    
                    cardMap.get(key).add(s2);
                } else {
    
    
                    List<String> list = new ArrayList<>();
                    list.add(s2);
                    cardMap.put(key, list);
                }
                continue;
            }
            int key = Integer.parseInt(s1);
            if (cardMap.containsKey(key)) {
    
    
                cardMap.get(key).add(s2);
            } else {
    
    
                List<String> list = new ArrayList<>();
                list.add(s2);
                cardMap.put(key, list);
            }
        }
        in.close();
        // 判断牌型
        // 同花顺 5 1 同花 5 4 顺子 5 5 其它 5 7
        // 四条 2 2 葫芦 2 3
        // 三条 3 6
        int res = 7;
        int len = cardMap.size();
        if (hasA) len -= 1;
        if (len == 3) {
    
    
            res = Math.min(res, 6);
        }
        if (len == 2) {
    
    
            for (Map.Entry<Integer, List<String>> item : cardMap.entrySet()) {
    
    
                int size = item.getValue().size();
                if (size == 1 || size == 4) {
    
    
                    res = Math.min(res, 2);
                } else {
    
    
                    res = Math.min(res, 3);
                }
            }
        }
        if (len == 5) {
    
    
            Boolean shunzi;
            // 判断是不是顺子
            List<Integer> collect = cardMap.keySet().stream().sorted().collect(Collectors.toList());
            if (hasA) {
    
    
                List<Integer> integers1 = collect.subList(0, 5);
                List<Integer> integers2 = collect.subList(1, 6);
                int i1 = integers1.get(4) - integers1.get(0);
                int i2 = integers2.get(4) - integers2.get(0);
                shunzi = i1 == 4 || i2 == 4;
            } else {
    
    
                int i = collect.get(4) - collect.get(0);
                shunzi = i == 4;
            }
            if (shunzi) res = Math.min(res, 5);
            // 判断是不是同花
            boolean tonghua = Boolean.FALSE;
            Set<String> set = new HashSet<>();
            cardMap.values().forEach(set::addAll);
            if (set.size() == 1){
    
    
                tonghua = true;
            }
            if (tonghua) {
    
    
                res = Math.min(res, 4);
            }
            if (tonghua && shunzi) {
    
    
                res = Math.min(res, 1);
            }
        }
        System.out.println(res);
    }
}

It's not too difficult a question, in fact, the analysis is in the code, just take a look and you'll understand.

Guess you like

Origin blog.csdn.net/qq_44503987/article/details/126454996