1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| public class Point24 {
public static void main(String[] args) { calculate(Lists.newArrayList(6, 6, 6, 6)).forEach(System.out::println); System.out.println();
calculate(Lists.newArrayList(1, 2, 1, 7)).forEach(System.out::println); System.out.println();
calculate(Lists.newArrayList(3, 3, 7, 7)).forEach(System.out::println); System.out.println();
calculate(Lists.newArrayList(3, 3, 8, 8)).forEach(System.out::println); System.out.println();
calculate(Lists.newArrayList(1, 5, 5, 5)).forEach(System.out::println); System.out.println();
calculate(Lists.newArrayList(1, 9, 8, 7)).forEach(System.out::println); System.out.println(); }
public static Set<String> calculate(List<Integer> numbers) { List<String> opList = new ArrayList<>(); opList.add("+"); opList.add("-"); opList.add("*"); opList.add("/");
List<List<Integer>> numberPerm = Permutation.permutation(numbers); List<List<String>> opSample = Sample.sample(opList, 3); return crossJoin(numberPerm, opSample); }
public static Set<String> crossJoin(List<List<Integer>> a, List<List<String>> b) { Set<String> expression = new HashSet<>(); a.forEach(ai -> { b.forEach(bi -> { List<String> result = cal(ai, bi); if (result != null) { expression.addAll(result); } }); });
return expression; }
private static List<String> cal(List<Integer> numbers, List<String> ops) { try { List<Exp> exps = numbers.stream().map(i -> new Exp(i.doubleValue())).collect(Collectors.toList()); List<Integer> opIndex = Lists.newArrayList(0, 1, 2); List<List<Integer>> permutation = Permutation.permutation(opIndex); return permutation.stream().map(oi -> { Integer first = oi.get(0); Integer second = oi.get(1); Integer third = oi.get(2);
Exp op1 = new Exp(ops.get(first), exps.get(first), exps.get(first + 1)); Exp op2; Exp op3 = new Exp(0.0); if (second - first == 1) { op2 = new Exp(ops.get(second), op1, exps.get(second + 1));
if (third > second) { op3 = new Exp(ops.get(third), op2, exps.get(third + 1)); } else { op3 = new Exp(ops.get(third), exps.get(third), op2); } } else if (second - first == -1) { op2 = new Exp(ops.get(second), exps.get(second), op1); if (third > second) { op3 = new Exp(ops.get(third), op2, exps.get(third + 1)); } else { op3 = new Exp(ops.get(third), exps.get(third), op2); } } else if (second - first == 2) { op2 = new Exp(ops.get(second), exps.get(second), exps.get(second + 1)); op3 = new Exp(ops.get(third), op1, op2); } else if (second - first == -2) { op2 = new Exp(ops.get(second), exps.get(second), exps.get(second + 1)); op3 = new Exp(ops.get(third), op2, op1); }
if (Math.abs(op3.value - 24) < 1e-5) { return op3.simpleString(); }
return null; }).filter(Objects::nonNull).collect(Collectors.toList());
} catch (Exception e) { }
return null; }
private static class Exp { private double value; private String exp;
public Exp(double value) { this.value = value; this.exp = String.valueOf((int) value); }
public Exp(String op, Exp a, Exp b) { value = op(op, a.value, b.value); exp = "(" + a.exp + op + b.exp + ")"; }
@Override public String toString() { return exp; }
public String simpleString() { if (exp.startsWith("(") && exp.endsWith(")")) { return exp.substring(1, exp.length() - 1); } else { return exp; } } }
private static double op(String op, Double a, Double b) { switch (op) { case "+": return a + b; case "-": return a - b; case "*": return a * b; case "/": return a / b; default: throw new RuntimeException("error op"); } }
}
|