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
| package com.demo.calcu.service;
import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.List;
@Service public class TwentyFourPointService {
public String solve(double[] a) { for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) { List<Double> res1 = new ArrayList<>(); res1.add(a[i] + a[j]); res1.add(a[i] - a[j]); res1.add(a[j] - a[i]); res1.add(a[i] * a[j]); res1.add(a[i] / a[j]); res1.add(a[j] / a[i]);
for (int u = 0; u < 4; u++) { if (u == i || u == j) continue; for (int k = 0; k < 6; k++) { List<Double> res2 = new ArrayList<>(); res2.add(res1.get(k) + a[u]); res2.add(res1.get(k) - a[u]); res2.add(a[u] - res1.get(k)); res2.add(res1.get(k) * a[u]); res2.add(res1.get(k) / a[u]); res2.add(a[u] / res1.get(k));
for (int v = 0; v < 4; v++) { if (v == i || v == j || v == u) continue; for (int h = 0; h < 6; h++) { List<Double> res3 = new ArrayList<>(); res3.add(res2.get(h) + a[v]); res3.add(res2.get(h) - a[v]); res3.add(a[v] - res2.get(h)); res3.add(res2.get(h) * a[v]); res3.add(res2.get(h) / a[v]); res3.add(a[v] / res2.get(h));
for (int g = 0; g < 6; g++) { if (Math.abs(res3.get(g) - 24) < 1e-6) { return buildExpression(a[i], a[j], a[u], a[v], k, h, g); } } } } } }
for (int u = 0; u < 4; u++) { if (u == i || u == j) continue; for (int v = 0; v < 4; v++) { if (v == i || v == j || v == u) continue; List<Double> res2 = new ArrayList<>(); res2.add(a[u] + a[v]); res2.add(a[u] - a[v]); res2.add(a[v] - a[u]); res2.add(a[u] * a[v]); res2.add(a[u] / a[v]); res2.add(a[v] / a[u]);
for (int k = 0; k < 6; k++) { for (int h = 0; h < 6; h++) { List<Double> res3 = new ArrayList<>(); res3.add(res1.get(k) + res2.get(h)); res3.add(res1.get(k) - res2.get(h)); res3.add(res2.get(h) - res1.get(k)); res3.add(res1.get(k) * res2.get(h)); res3.add(res1.get(k) / res2.get(h)); res3.add(res2.get(h) / res1.get(k));
for (int g = 0; g < 6; g++) { if (Math.abs(res3.get(g) - 24) < 1e-6) { return buildExpression(a[i], a[j], a[u], a[v], k, h, g); } } } } } } } } return "No solution found."; }
private String buildExpression(double a, double b, double c, double d, int op1, int op2, int op3) { String o1 = String.valueOf((int) a); String o2 = String.valueOf((int) b); String o3 = String.valueOf((int) c); String o4 = String.valueOf((int) d);
String exp1 = applyOperation(o1, o2, op1); String exp2 = applyOperation(exp1, o3, op2); return applyOperation(exp2, o4, op3); }
private String applyOperation(String x, String y, int op) { switch (op) { case 0: return "(" + x + " + " + y + ")"; case 1: return "(" + x + " - " + y + ")"; case 2: return "(" + y + " - " + x + ")"; case 3: return "(" + x + " * " + y + ")"; case 4: return "(" + x + " / " + y + ")"; case 5: return "(" + y + " / " + x + ")"; default: return ""; } } }
|