题解 | #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

相关推荐

10-15 15:00
潍坊学院 golang
跨考小白:这又不是官方
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
ProMonkey2024:5个oc?厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了(别的帖子偷来的,现学现卖😋)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务