[Java] 自 动 论 证 机

NumberNode.class
final class NumberNode {
    String str;
    int numerator;
    int denominator;
    NumberNode next;

    NumberNode() {
    }

    NumberNode(String str, int fenzi, int fenmu, NumberNode next) {
        this.str = str;
        this.numerator = fenzi;
        this.denominator = fenmu;
        this.next = next;
    }

    boolean whatINeed() {
        return numerator % denominator == 0 && (numerator ^ denominator) >> 31 == 0;
    }

    int intVal() {
        return numerator / denominator;
    }

    static NumberNode defaults(int val) {
        return new NumberNode(Integer.toString(val), val, 1, null);
    }
}

Calcer.class

final class Calcer {
    Calcer() {
    }

    private StringBuilder sb = new StringBuilder();

    private String tos(NumberNode a, char theta, NumberNode b) {
        sb.setLength(0);
        return sb.append('(').append(a.str).append(theta).append(b.str).append(')').toString();
    }

    void add(NumberNode a, NumberNode b, NumberNode equal) {
        equal.str = tos(a, '+', b);
        equal.numerator = a.numerator * b.denominator + b.numerator * a.denominator;
        equal.denominator = a.denominator * b.denominator;
    }

    void sub(NumberNode a, NumberNode b, NumberNode equal) {
        equal.str = tos(a, '-', b);
        equal.numerator = a.numerator * b.denominator - b.numerator * a.denominator;
        equal.denominator = a.denominator * b.denominator;
    }

    void mul(NumberNode a, NumberNode b, NumberNode equal) {
        equal.str = tos(a, '*', b);
        equal.numerator = a.numerator * b.numerator;
        equal.denominator = a.denominator * b.denominator;
    }

    void div(NumberNode a, NumberNode b, NumberNode equal) {
        equal.str = tos(a, '/', b);
        equal.numerator = a.numerator * b.denominator;
        equal.denominator = a.denominator * b.numerator;
    }
}

Proofer.class

public final class Proofer {
    public Proofer() {
    }

    private Calcer calc = new Calcer();

    public TreeMap<Integer, String> list(int... nums) {
        var list = new TreeMap<Integer, String>();
        var head = new NumberNode();
        var now = head;
        for (var i : nums) {
            now.next = NumberNode.defaults(i);
            now = now.next;
        }
        func(list, head);
        return list;
    }

    void func(Map<Integer, String> map, NumberNode head) {
        if (head.next.next == null) {// only 1 node
            if (head.next.whatINeed()) {
                map.putIfAbsent(head.next.intVal(), head.next.str);
            }
            return;
        }
        var ps = new NumberNode[] { head, new NumberNode(), null };// [0]=now, [1]=temp1, [2]=temp2
        while (ps[0].next.next != null) {
            calc.add(ps[0].next, ps[0].next.next, ps[1]);
            subfunc(map, head, ps);

            calc.sub(ps[0].next, ps[0].next.next, ps[1]);
            subfunc(map, head, ps);

            calc.mul(ps[0].next, ps[0].next.next, ps[1]);
            subfunc(map, head, ps);

            if (ps[0].next.next.intVal() != 0) {
                calc.div(ps[0].next, ps[0].next.next, ps[1]);
                subfunc(map, head, ps);
            }

            ps[0] = ps[0].next;
        }
    }

    void subfunc(Map<Integer, String> map, NumberNode head, NumberNode[] ps) {
        ps[2] = ps[0].next;
        ps[0].next = ps[1];
        ps[1].next = ps[2].next.next;
        func(map, head);
        ps[0].next = ps[2];
    }
}

使 用 例

var map = new Proofer().list(1, 1, 4, 5, 1, 4);
for (int first = map.firstEntry().getKey(), last = map.lastEntry().getKey() + 1; first < last; first++) {
    System.out.printf("%d = %s%n",first,map.get(first));
}
0 = (((((1+1)+4)-5)-1)*4)
1 = (((((1+1)-4)+5)+1)/4)
2 = (((1+1)/4)+((5+1)/4))
3 = (((((1+1)+4)+5)+1)/4)
4 = (((((1+1)+4)-5)-1)+4)
5 = (((((1+1)+4)-5)*1)+4)
6 = (((((1+1)+4)+5)-1)-4)
7 = (((((1+1)+4)+5)*1)-4)
8 = (((((1+1)+4)+5)+1)-4)
9 = ((((1+1)+4)*(5+1))/4)
10 = (((((1+1)*4)+5)+1)-4)
11 = ((1+1)*(4+((5+1)/4)))
12 = (((1+1)+4)*((5+1)-4))
13 = (((1+1)-4)-(5*(1-4)))
14 = (((((1+1)+4)+5)-1)+4)
15 = (((((1+1)+4)+5)*1)+4)
16 = (((((1+1)+4)+5)+1)+4)
17 = (((((1+1)*4)+5)*1)+4)
18 = (((1+1)-4)+((5*1)*4))
19 = ((((1+1)+(4*5))+1)-4)
20 = ((((1+1)+4)*(5-1))-4)
21 = (((1+1)+4)-(5*(1-4)))
22 = (((1+1)+4)+((5-1)*4))
23 = (((1+1)-4)+(5*(1+4)))
24 = (((1+1)*4)+((5-1)*4))
25 = (((((1+1)+4)*5)-1)-4)
26 = (((((1+1)+4)*5)*1)-4)
27 = (((((1+1)+4)*5)+1)-4)
28 = ((((1+1)+4)*(5-1))+4)
29 = ((1+1)-((4+5)*(1-4)))
30 = (((1+1)+4)+((5+1)*4))
31 = (((1+1)+4)+(5*(1+4)))
32 = ((((1+1)+4)*(5+1))-4)
33 = (((((1+1)+4)*5)-1)+4)
34 = (((((1+1)+4)*5)*1)+4)
35 = (((((1+1)+4)*5)+1)+4)
36 = (((((1+1)*4)*5)*1)-4)
37 = (((((1+1)*4)*5)+1)-4)
38 = (((1+1)*4)*(5-(1/4)))
39 = null
40 = (((((1+1)+4)+5)-1)*4)
41 = ((1*1)+(((4+5)+1)*4))
42 = (((1+1)*4)*(5+(1/4)))
43 = (((((1+1)*4)*5)-1)+4)
44 = (((((1+1)+4)+5)*1)*4)
45 = (((((1+1)*4)*5)+1)+4)
46 = (((1+1)*((4*5)+1))+4)
47 = ((1+1)+((4+5)*(1+4)))
48 = (((((1+1)+4)+5)+1)*4)
49 = (1-(((1-4)*(5-1))*4))
50 = ((1+1)*(((4*5)+1)+4))
51 = (1+(((1+4)+5)*(1+4)))
52 = (((((1+1)*4)+5)*1)*4)
53 = null
54 = (((1+1)+4)*((5*1)+4))
55 = ((((1+1)+4)+5)*(1+4))
56 = (((((1+1)*4)+5)+1)*4)
57 = (((1*1)-(4*5))*(1-4))
58 = ((1+1)*(4+(5*(1+4))))
59 = null
60 = (((1+1)+4)*((5+1)+4))
61 = ((1*1)-((4*5)*(1-4)))
62 = ((1+1)-((4*5)*(1-4)))
63 = null
64 = (((1+1)*4)*((5-1)+4))
65 = ((((1+1)*4)+5)*(1+4))
66 = ((1+1)+((4*(5-1))*4))
67 = null
68 = ((((1+1)*(4+5))-1)*4)
69 = (1+((1+(4*(5-1)))*4))
70 = null
71 = null
72 = (((1+1)*4)*((5*1)+4))
73 = (1-(((1-4)*(5+1))*4))
74 = null
75 = null
76 = ((((1+1)*(4+5))+1)*4)
77 = ((1*1)+(((4*5)-1)*4))
78 = ((1+1)+(((4*5)-1)*4))
79 = null
80 = (((1+1)*4)*((5+1)+4))
81 = ((1*1)+(((4*5)*1)*4))
82 = ((1+1)+(((4*5)*1)*4))
83 = null
84 = ((((1+1)+(4*5))-1)*4)
85 = ((1*1)+(((4*5)+1)*4))
86 = ((1+1)+(((4*5)+1)*4))
87 = null
88 = ((((1+1)+(4*5))*1)*4)
89 = (1+(((1+(4*5))+1)*4))
90 = (((1+1)*(4+5))*(1+4))
91 = null
92 = ((((1+1)+(4*5))+1)*4)
93 = (1-((1-(4*(5+1)))*4))
94 = null
95 = null
96 = ((((1+1)+4)*(5-1))*4)
97 = ((1*1)+((4*(5+1))*4))
98 = ((1+1)+((4*(5+1))*4))
99 = null
100 = ((((1-1)+4)*5)*(1+4))
101 = ((1*1)+((4*5)*(1+4)))
102 = ((1+1)+((4*5)*(1+4)))
103 = null
104 = (((1+1)+(4*(5+1)))*4)
105 = (((1*1)+(4*5))*(1+4))
106 = (1+((1+(4*5))*(1+4)))
107 = null
108 = (((1+((1+4)*5))+1)*4)
109 = null
110 = (((1+1)+(4*5))*(1+4))
111 = null
112 = null
113 = null
114 = null
115 = null
116 = (((((1+1)+4)*5)-1)*4)
117 = null
118 = null
119 = null
120 = (((((1+1)+4)*5)*1)*4)
121 = (1+(((1+4)*(5+1))*4))
122 = null
123 = null
124 = (((((1+1)+4)*5)+1)*4)
125 = ((((1*1)+4)*5)*(1+4))
126 = (1+(((1+4)*5)*(1+4)))
127 = null
128 = ((((1+1)*4)*(5-1))*4)
129 = null
130 = ((1+((1+4)*5))*(1+4))
131 = null
132 = null
133 = null
134 = null
135 = null
136 = null
137 = null
138 = null
139 = null
140 = null
141 = null
142 = null
143 = null
144 = ((((1+1)+4)*(5+1))*4)
145 = null
146 = null
147 = null
148 = null
149 = null
150 = ((((1+1)+4)*5)*(1+4))
151 = null
152 = (((1+1)*((4*5)-1))*4)
153 = null
154 = null
155 = null
156 = (((((1+1)*4)*5)-1)*4)
157 = null
158 = null
159 = null
160 = (((((1+1)*4)*5)*1)*4)
161 = null
162 = null
163 = null
164 = (((((1+1)*4)*5)+1)*4)
165 = null
166 = null
167 = null
168 = (((1+1)*((4*5)+1))*4)
169 = null
170 = null
171 = null
172 = null
173 = null
174 = null
175 = null
176 = null
177 = null
178 = null
179 = null
180 = null
181 = null
182 = null
183 = null
184 = null
185 = null
186 = null
187 = null
188 = null
189 = null
190 = null
191 = null
192 = ((((1+1)*4)*(5+1))*4)
193 = null
194 = null
195 = null
196 = null
197 = null
198 = null
199 = null
200 = ((((1+1)*4)*5)*(1+4))

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注