题解 | #24点运算#

24点运算

http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d

import java.util.*;

public class Main {
    private static Map<String, Integer> map = new HashMap<String, Integer>() {
        {
            put("3", 3);
            put("4", 4);
            put("5", 5);
            put("6", 6);
            put("7", 7);
            put("8", 8);
            put("9", 9);
            put("10", 10);
            put("J", 11);
            put("Q", 12);
            put("K", 13);
            put("A", 1);
            put("2", 2);
        }
    };

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if (line.contains("joker") || line.contains("JOKER")) {
                System.out.println("ERROR");
                continue;
            }
            String[] split = line.split(" ");
            // 获取牌面实际代表的数值
            int[] cards = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                cards[i] = map.get(split[i]);
            }
            // 标记是否能计算出24
            boolean flag = false;
            if (cards[0] + cards[1] + cards[2] + cards[3] == 24) {
                System.out.println(split[0] + "+" + split[1] + "+" + split[2] + "+" + split[3]);
                flag = true;
            } else {
                List<String> calGroup = getCalculateGroup();
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        if (i == j) {
                            continue;
                        }
                        for (int k = 0; k < 4; k++) {
                            if (k == i || k == j) {
                                continue;
                            }
                            for (int h = 0; h < 4; h++) {
                                if (h == k || h == i || h == j) {
                                    continue;
                                }
                                // 穷举所有数字组合
                                if (!flag && calculate(split, i, j, k, h, calGroup)) {
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (!flag) {
                System.out.println("NONE");
            }
        }
        sc.close();
    }

    private static List<String> getCalculateGroup() {
        String[] sign = {"+", "-", "*", "/"};
        // 算法组合
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (i == j) {
                    continue;
                }
                for (int k = 0; k < 4; k++) {
                    if (k == i || k == j) {
                        continue;
                    }
                    // 穷举算法组合
                    String signCombine = sign[i] + sign[j] + sign[k];
                    if (!list.contains(signCombine)) {
                        list.add(signCombine);
                    }
                }
            }
        }
        return list;
    }

    private static boolean calculate(String[] split, int i, int j, int k, int h, List<String> calGroup) {
        int card = map.get(split[i]);
        int card1 = map.get(split[j]);
        int card2 = map.get(split[k]);
        int card3 = map.get(split[h]);
        // 不同的算术符号组合
        if (card - card1 - card2 - card3 == 24) {
            System.out.println(split[i] + "-" + split[j] + "-" + split[k] + "-" + split[h]);
            return true;
        }
        if (card * card1 * card2 * card3 == 24) {
            System.out.println(split[i] + "*" + split[j] + "*" + split[k] + "*" + split[h]);
            return true;
        }
        if (card / card1 / card2 / card3 == 24) {
            System.out.println(split[i] + "/" + split[j] + "/" + split[k] + "/" + split[h]);
            return true;
        }
        for (String s : calGroup) {
            int result = 0;
            if (s.charAt(0) == '+') {
                result = card + card1;
            } else if (s.charAt(0) == '-') {
                result = card - card1;
            } else if (s.charAt(0) == '*') {
                result = card * card1;
            } else if (s.charAt(0) == '/') {
                result = card / card1;
            }
            if (s.charAt(1) == '+') {
                result = result + card2;
            } else if (s.charAt(1) == '-') {
                result = result - card2;
            } else if (s.charAt(1) == '*') {
                result = result * card2;
            } else if (s.charAt(1) == '/') {
                result = result / card2;
            }
            if (s.charAt(2) == '+') {
                result = result + card3;
            } else if (s.charAt(2) == '-') {
                result = result - card3;
            } else if (s.charAt(2) == '*') {
                result = result * card3;
            } else if (s.charAt(2) == '/') {
                result = result / card3;
            }
            if (result == 24) {
                System.out.println(split[i] + s.charAt(0) + split[j] + s.charAt(1) + split[k] + s.charAt(2) + split[h]);
                return true;
            }
        }
        return false;
    }
}
全部评论

相关推荐

Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务