题解 | #24点运算#递归全排列

24点运算

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

import java.math.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.stream.*;
import java.util.regex.*;
import java.util.function.*;

public class Main {
    static Map<String, IntBinaryOperator> ops = new HashMap<>();
    static {
        ops.put("+", (a, b)->a + b);
        ops.put("-", (a, b)->a - b);
        ops.put("*", (a, b)->a * b);
        ops.put("/", (a, b)->a / b);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Integer> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            String next = in.next();
            if ("joker".equals(next.toLowerCase())) {
                System.out.println("ERROR");
                return;
            }
            if ("J".equals(next)) {
                pokers.add(11);
                continue;
            }
            if ("Q".equals(next)) {
                pokers.add(12);
                continue;
            }
            if ("K".equals(next)) {
                pokers.add(13);
                continue;
            }
            if ("A".equals(next)) {
                pokers.add(1);
                continue;
            }

            pokers.add(Integer.parseInt(next));
        }

        List<List<String>> allWays = new ArrayList<>();
        point24(pokers, 0, new ArrayList<>(), allWays);

        System.out.println(
        allWays.size() == 0 ? "NONE" : allWays.get(0).stream().map(s-> {
            if ("11".equals(s)) return "J";
            if ("12".equals(s)) return "Q";
            if ("13".equals(s)) return "K";
            if ("1".equals(s)) return "A";
            return s;
        })
        .collect(Collectors.joining("")));
    }

    static void point24(List<Integer> pokers, int result,
                        List<String> way,
                        List<List<String>> allWays) {
        if (pokers.size() == 0) {
            if (result == 24) {
                allWays.add(way);
            } else {
                return;
            }
        }

        if (pokers.size() == 4) {
            for (int i = 0; i < pokers.size(); i++) {
                List<Integer> copy = new ArrayList<>(pokers);
                copy.remove(i);
                List<String> newWay = new ArrayList<>(way);
                newWay.add(String.valueOf(pokers.get(i)));
                point24(copy, pokers.get(i), newWay, allWays);
            }
        } else {
            for (int i = 0; i < pokers.size(); i++) {
                for (Map.Entry<String, IntBinaryOperator> entry : ops.entrySet()) {
                    List<Integer> copy = new ArrayList<>(pokers);
                    copy.remove(i);
                    List<String> newWay = new ArrayList<>(way);
                    newWay.add(entry.getKey());
                    newWay.add(String.valueOf(pokers.get(i)));

                    int newResult = entry.getValue().applyAsInt(result, pokers.get(i));
                    point24(copy, newResult, newWay, allWays);
                }
            }
        }
    }
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务