题解 | #24点运算#

24点运算

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

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] strs = scan.nextLine().split(" ");
        HashMap<String, Integer> StringToInteger = new HashMap<>();
        HashMap<Integer, String> IntegerToString = new HashMap<>();
        ArrayList<Integer> nums = new ArrayList<>();
        for (int i = 2; i <= 10; i++) {
            StringToInteger.put(String.valueOf(i), i);
            IntegerToString.put(i, String.valueOf(i));
        }
        StringToInteger.put("A", 1);
        StringToInteger.put("J", 11);
        StringToInteger.put("Q", 12);
        StringToInteger.put("K", 13);
        IntegerToString.put(1, "A");
        IntegerToString.put(11, "J");
        IntegerToString.put(12, "Q");
        IntegerToString.put(13, "K");
        for (String str : strs) {
            if ("joker".equals(str) || "JOKER".equals(str)) {
                System.out.println("ERROR");
                return;
            }
            nums.add(StringToInteger.get(str));
        }
        for (int num : nums) {
            ArrayList<Integer> copyArr = new ArrayList<>(nums);
            copyArr.remove(Integer.valueOf(num));
            // String tmp = process(String.valueOf(IntegerToString.get(num)), String.valueOf(num), copyArr, IntegerToString);
            String tmp = process(String.valueOf(IntegerToString.get(num)), num, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                System.out.println(tmp);
                return;
            }
        }
        System.out.println("NONE");
    }
    
    /**********************************************************************************/
    public static String process(String previousStr, int previousTotal, ArrayList<Integer> nums, HashMap<Integer, String> IntegerToString) {
        if (nums.size() == 0) {
            return previousTotal == 24 ? previousStr : "NONE";
        }
        for (int i = 0; i < nums.size(); i++) {
            int current = nums.get(i);
            ArrayList<Integer> copyArr = new ArrayList<>(nums);
            copyArr.remove(Integer.valueOf(current));
            String tmp = process(previousStr + "+" + IntegerToString.get(current), previousTotal + current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
            tmp = process(previousStr + "-" + IntegerToString.get(current), previousTotal - current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
            tmp = process(previousStr + "*" + IntegerToString.get(current), previousTotal * current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
            tmp = process(previousStr + "/" + IntegerToString.get(current), previousTotal / current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
        }
        return "NONE";
    }
    
    /**********************************************************************************/
    /*
    public static String process(String previousStr, String numsStr, ArrayList<Integer> nums, HashMap<Integer, String> IntegerToString) {
        if (nums.size() == 0) {
            return eval(numsStr) == 24 ? previousStr : "NONE";
        }
        for (int i = 0; i < nums.size(); i++) {
            int current = nums.get(i);
            ArrayList<Integer> copyArr = new ArrayList<>(nums);
            copyArr.remove(Integer.valueOf(current));
            String tmp = process(previousStr + "+" + IntegerToString.get(current), numsStr + "+" + current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
            tmp = process(previousStr + "-" + IntegerToString.get(current), numsStr + "-" + current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
            tmp = process(previousStr + "*" + IntegerToString.get(current), numsStr + "*" + current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
            tmp = process(previousStr + "/" + IntegerToString.get(current), numsStr + "/" + current, copyArr, IntegerToString);
            if (!tmp.equals("NONE")) {
                return tmp;
            }
        }
        return "NONE";
    }
    public static int eval(String str) {
        String[] strs = str.split("");
        LinkedList<String> ll = new LinkedList<>();
        for (String tmp : strs) {
            if ("+".equals(tmp) || "-".equals(tmp) || "*".equals(tmp) || "/".equals(tmp)) {
                ll.add(tmp);
            } else {
                if (!ll.isEmpty() && !"+".equals(ll.peekLast()) && !"-".equals(ll.peekLast()) && !"*".equals(ll.peekLast()) && !"/".equals(ll.peekLast())) {
                    String last = ll.peekLast();
                    ll.removeLast();
                    ll.add(last + tmp);
                } else {
                    ll.add(tmp);
                }
            }
        }
        Stack<String> stack = new Stack<>();
        LinkedList<String> suffix = new LinkedList<>();
        while (!ll.isEmpty()) {
            String tmp = ll.poll();
            if (!("+".equals(tmp) || "-".equals(tmp) || "*".equals(tmp) || "/".equals(tmp))) {
                suffix.add(tmp);
            } else {
                if ("*".equals(tmp) || "/".equals(tmp)) {
                    while (!stack.isEmpty() && !(stack.peek().equals("+") || stack.peek().equals("-"))) {
                        suffix.add(stack.pop());
                    }
                    stack.push(tmp);
                } else {
                    while (!stack.isEmpty()) {
                        suffix.add(stack.pop());
                    }
                    stack.push(tmp);
                }
            }
        }
        while (!stack.isEmpty()) {
            suffix.add(stack.pop());
        }
        int ans = 0;
        while (!suffix.isEmpty()) {
            String tmp = suffix.poll();
            if ("+".equals(tmp) || "-".equals(tmp) || "*".equals(tmp) || "/".equals(tmp)) {
                int num1 = Integer.valueOf(stack.pop());
                int num2 = Integer.valueOf(stack.pop());
                if ("+".equals(tmp)) {
                    ans = num2 + num1;
                } else if ("-".equals(tmp)) {
                    ans = num2 - num1;
                } else if ("*".equals(tmp)) {
                    ans = num2 * num1;
                } else {
                    ans = num2 / num1;
                }
                stack.push(String.valueOf(ans));
            } else {
                stack.push(tmp);
            }
        }
        return Integer.valueOf(stack.pop());
    }
    */
}
全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-26 19:06

相关推荐

11-30 11:07
河南大学 Java
宇宙厂 测开 n*15
丘丘给个offer:有后选后
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-29 12:19
点赞 评论 收藏
分享
沉淀一会:1.同学你面试评价不错,概率很大,请耐心等待; 2.你的排名比较靠前,不要担心,耐心等待; 3.问题不大,正在审批,不要着急签其他公司,等等我们! 4.预计9月中下旬,安心过节; 5.下周会有结果,请耐心等待下; 6.可能国庆节前后,一有结果我马上通知你; 7.预计10月中旬,再坚持一下; 8.正在走流程,就这两天了; 9.同学,结果我也不知道,你如果查到了也告诉我一声; 10.同学你出线不明朗,建议签其他公司保底! 11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务