24点 算法

一、具体问题

​ 从扑克中每次取出4张牌。使用加减乘除,第一个能得出24者为赢。(其中,J代表11,Q代表12,K代表13,A代表1),按照要求编程解决24点游戏。

二、算法分析

​ 采用穷举法列举每一种存在的可能,接着判断是否值为24,如果等于24,则将表达式存入Set集合,最终遍历Set集合即可得到所有表达式。具体思路如下:

  1. 采用随机数生成4个符合要求的数据,假设四个数据为n1,n2,n3,n4 。

  2. 把数据相互组合可以得到如下组合:n1和n2 ,n1和n3,n1和n4,n2和n3,n2和n4,n3和n4

  3. 将上面的组合进行各种可能的运算例如:n1+n2,n1-n2,n2-n1,n1*n2,n1/n2,n2/n1等等。

  4. 把上面组合计算出来的结果存入进对应的数组中例如:组合相加的结果存入add数组,相减的结果存入sub数组……最终将这些数组存入一个list集合中,目的是为了方便通过循环遍历出每一种组合。

  5. 通过循环去遍历每一种组合,把这些组合在一起进行相加,相减等运算,记录结果为24的表达式。在这里需要注意的是,因为数组得值为两个数字的运算结果,所以需要根据当前循环变量的值和list集合以及数组存入数据的顺序去把表达式格式化成四个数字组成的表达式,否则表达式只有两个数字。

需要注意的是:在遍历集合的过程中,由于集合中存入的数组的数据为两个数据组合的形式,所以遍历是只需要控制好下标,使的每一个表达式中只有n1,n2,n3,n4这四个数据,而不会出现类似于n1,n2,n1,n3 这种组合的方式。

具体的流程图入下图所示:

20180930135645306

三、代码实现

Game.java

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
142
143
144
145
146
147
148
149
150
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

public class Game {
static Set<String> set = new HashSet<String>();
// 使用Set存储算式表达式可以避免有重复的结果
float[] add = new float[6];
// 存放组合相加的结果的集合
float[] sub1 = new float[6];
// 存放相减的结果(两个数相减的结果存在两种可能)
float[] sub2 = new float[6];
// 存放相减的结果
float[] mul = new float[6];
// 存放相乘
float[] div1 = new float[6];
// 存放相除(两个数相除的结果存在两种可能)
float[] div2 = new float[6];
// 存放相除
List<float[]> list = new ArrayList<float[]>();
// 存放组合结果运算的集合

public void conbine(float n1, float n2, float n3, float n4) {
add[0] = n1 + n2;
add[1] = n1 + n3;
add[2] = n1 + n4;
add[3] = n2 + n3;
add[4] = n2 + n4;
add[5] = n3 + n4;
sub1[0] = n1 - n2;
sub1[1] = n1 - n3;
sub1[2] = n1 - n4;
sub1[3] = n2 - n3;
sub1[4] = n2 - n4;
sub1[5] = n3 - n4;
sub1[5] = n3 + n4;
sub2[0] = n2 - n1;
sub2[1] = n3 - n1;
sub2[2] = n4 - n1;
sub2[3] = n3 - n2;
sub2[4] = n4 - n2;
sub2[5] = n4 - n3;
mul[0] = n2 * n1;
mul[1] = n3 * n1;
mul[2] = n4 * n1;
mul[3] = n3 * n2;
mul[4] = n4 * n2;
mul[5] = n4 * n3;
div1[0] = n1 / n2;
div1[1] = n1 / n3;
div1[2] = n1 / n4;
div1[3] = n2 / n3;
div1[4] = n2 / n4;
div1[5] = n3 / n4;
div2[0] = n2 / n1;
div2[1] = n3 / n1;
div2[2] = n4 / n1;
div2[3] = n3 / n2;
div2[4] = n4 / n2;
div2[5] = n4 / n3;
list.add(add);
// 把各种组合加入到list集合中,方便通过循环来遍历每一种组合方式
list.add(sub1);
list.add(sub2);
list.add(mul);
list.add(div1);
list.add(div2);
}

public void getData(float[] data) {
System.out.print("四个数字为:");
for (float f : data) {
switch ((int) f) {// 将11,12,13,1变成J,Q,K,A
case 1:
System.out.print("A" + " ");
break;
case 11:
System.out.print("J" + " ");
break;
case 12:
System.out.print("Q" + " ");
break;
case 13:
System.out.print("K" + " ");
break;
default:
System.out.print((int) f + " ");
break;
}
}
System.out.println();
boolean flag = false;// 通过该变量去判断是否存在表达式
conbine(data[0], data[1], data[2], data[3]);
for (int a = 0; a < 3; a++) {// 有3种组合方式,分别遍历每一种组合方法
for (int b = 0; b < 6; b++) {// 穷举每一个组合和他们之间的运算
for (int c = 0; c < 6; c++) {// 判断每一种组合的每一种运算结果是否等于24
if ((list.get(b)[a] + list.get(c)[5 - a]) == 24) {
DataFormat.judge(a, b, c, data, "+", set);
flag = true;
}
// 减法
if ((list.get(b)[a] - list.get(c)[5 - a]) == 24) {
DataFormat.judge(a, b, c, data, "-", set);
}
if ((list.get(b)[5 - a] - list.get(c)[a]) == 24) {
DataFormat.judge(a, b, c, data, "-", set);
flag = true;
}
// 乘法
if ((list.get(b)[a] * list.get(c)[5 - a]) == 24) {
DataFormat.judge(a, b, c, data, "*", set);
flag = true;
}
// 除法
if ((list.get(b)[a] / list.get(c)[5 - a]) == 24) {
DataFormat.judge(a, b, c, data, "/", set);
flag = true;
}
if ((list.get(b)[5 - a] / list.get(c)[a]) == 24) {
DataFormat.judge(a, b, c, data, "/", set);
flag = true;
}
}
}
}
if (!flag) {
System.out.println("没有表达式满足条件");
}else{
for (String str : set) {
System.out.println(str.replace(".0", " "));
}
}
}

public static void main(String[] args) throws IOException {
Game g = new Game();
float[] data = new float[4];
Random r = new Random();
// 获取1——13的的数字的集合
data[0] = r.nextInt(12) + 1;
data[1] = r.nextInt(12) + 1;
data[2] = r.nextInt(12) + 1;
data[3] = r.nextInt(12) + 1;
g.getData(data);

}
}

DataFormat.java

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import java.util.Set;

public class DataFormat {
public static void judge(int a,int b,int c,float[] n,String op,Set<String> set) {
// 格式化输出表达式
StringBuilder sb = new StringBuilder();
if(a==0) {//n0和n1 与n2和n3之间的运算
if(b==0) {//n0+n1
if(c==0) {//n2+n3
sb.append("(").append(n[0]).append("+").append(n[1]).append(")").append(op).append("(").append(n[2]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("+").append(n[1]).append(")").append(op).append("(").append(n[2]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("+").append(n[1]).append(")").append(op).append("(").append(n[3]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("+").append(n[1]).append(")").append(op).append("(").append(n[2]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("+").append(n[1]).append(")").append(op).append("(").append(n[2]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("+").append(n[1]).append(")").append(op).append("(").append(n[3]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==1) {//n0-n1
if(c==0) {//n2+n3
sb.append("(").append(n[0]).append("-").append(n[1]).append(")").append(op).append("(").append(n[2]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("-").append(n[1]).append(")").append(op).append("(").append(n[2]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("-").append(n[1]).append(")").append(op).append("(").append(n[3]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("-").append(n[1]).append(")").append(op).append("(").append(n[2]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("-").append(n[1]).append(")").append(op).append("(").append(n[2]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("-").append(n[1]).append(")").append(op).append("(").append(n[3]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==2) {//n1-n0
if(c==0) {//n2+n3
sb.append("(").append(n[1]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[1]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[1]).append("-").append(n[0]).append(")").append(op).append("(").append(n[3]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[1]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[1]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[1]).append("-").append(n[0]).append(")").append(op).append("(").append(n[3]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==3) {//n0*n1
if(c==0) {//n2+n3
sb.append("(").append(n[0]).append("*").append(n[1]).append(")").append(op).append("(").append(n[2]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("*").append(n[1]).append(")").append(op).append("(").append(n[2]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("*").append(n[1]).append(")").append(op).append("(").append(n[3]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("*").append(n[1]).append(")").append(op).append("(").append(n[2]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("*").append(n[1]).append(")").append(op).append("(").append(n[2]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("*").append(n[1]).append(")").append(op).append("(").append(n[3]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==4) {//n0/n1
if(c==0) {//n2+n3
sb.append("(").append(n[0]).append("/").append(n[1]).append(")").append(op).append("(").append(n[2]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("/").append(n[1]).append(")").append(op).append("(").append(n[2]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("/").append(n[1]).append(")").append(op).append("(").append(n[3]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("/").append(n[1]).append(")").append(op).append("(").append(n[2]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("/").append(n[1]).append(")").append(op).append("(").append(n[2]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("/").append(n[1]).append(")").append(op).append("(").append(n[3]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==5) {//n1/n0
if(c==0) {//n2+n3
sb.append("(").append(n[1]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[1]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[1]).append("/").append(n[0]).append(")").append(op).append("(").append(n[3]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[1]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[1]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[1]).append("/").append(n[0]).append(")").append(op).append("(").append(n[3]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}
}
if(a==1) {//n0和n2 与n1和n3之间的运算
if(b==0) {//n0+n2
if(c==0) {//n1+n3
sb.append("(").append(n[0]).append("+").append(n[2]).append(")").append(op).append("(").append(n[1]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("+").append(n[2]).append(")").append(op).append("(").append(n[1]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("+").append(n[2]).append(")").append(op).append("(").append(n[3]).append("-").append(n[1]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("+").append(n[2]).append(")").append(op).append("(").append(n[1]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("+").append(n[2]).append(")").append(op).append("(").append(n[1]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("+").append(n[2]).append(")").append(op).append("(").append(n[3]).append("/").append(n[1]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==1) {//n0-n2
if(c==0) {//n1+n3
sb.append("(").append(n[0]).append("-").append(n[2]).append(")").append(op).append("(").append(n[1]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("-").append(n[2]).append(")").append(op).append("(").append(n[1]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("-").append(n[2]).append(")").append(op).append("(").append(n[3]).append("-").append(n[1]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("-").append(n[2]).append(")").append(op).append("(").append(n[1]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("-").append(n[2]).append(")").append(op).append("(").append(n[1]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("-").append(n[2]).append(")").append(op).append("(").append(n[3]).append("/").append(n[1]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==2) {//n1-n2
if(c==0) {//n1+n3
sb.append("(").append(n[2]).append("-").append(n[0]).append(")").append(op).append("(").append(n[1]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[2]).append("-").append(n[0]).append(")").append(op).append("(").append(n[1]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[2]).append("-").append(n[0]).append(")").append(op).append("(").append(n[3]).append("-").append(n[1]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[2]).append("-").append(n[0]).append(")").append(op).append("(").append(n[1]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[2]).append("-").append(n[0]).append(")").append(op).append("(").append(n[1]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[2]).append("-").append(n[0]).append(")").append(op).append("(").append(n[3]).append("/").append(n[1]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==3) {//n0*n2
if(c==0) {//n1+n3
sb.append("(").append(n[0]).append("*").append(n[2]).append(")").append(op).append("(").append(n[1]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("*").append(n[2]).append(")").append(op).append("(").append(n[1]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("*").append(n[2]).append(")").append(op).append("(").append(n[3]).append("-").append(n[1]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("*").append(n[2]).append(")").append(op).append("(").append(n[1]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("*").append(n[2]).append(")").append(op).append("(").append(n[1]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("*").append(n[2]).append(")").append(op).append("(").append(n[3]).append("/").append(n[1]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==4) {//n0/n2
if(c==0) {//n1+n3
sb.append("(").append(n[0]).append("/").append(n[2]).append(")").append(op).append("(").append(n[1]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("/").append(n[2]).append(")").append(op).append("(").append(n[1]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("/").append(n[2]).append(")").append(op).append("(").append(n[3]).append("-").append(n[1]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("/").append(n[2]).append(")").append(op).append("(").append(n[1]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("/").append(n[2]).append(")").append(op).append("(").append(n[1]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("/").append(n[2]).append(")").append(op).append("(").append(n[3]).append("/").append(n[1]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==5) {//n2/n0
if(c==0) {//n1+n3
sb.append("(").append(n[2]).append("/").append(n[0]).append(")").append(op).append("(").append(n[1]).append("+").append(n[3]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[2]).append("/").append(n[0]).append(")").append(op).append("(").append(n[1]).append("-").append(n[3]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[2]).append("/").append(n[0]).append(")").append(op).append("(").append(n[3]).append("-").append(n[1]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[2]).append("/").append(n[0]).append(")").append(op).append("(").append(n[1]).append("*").append(n[3]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[2]).append("/").append(n[0]).append(")").append(op).append("(").append(n[1]).append("/").append(n[3]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[2]).append("/").append(n[0]).append(")").append(op).append("(").append(n[3]).append("/").append(n[1]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}
}
if(a==2) {//n0和n3 与n2和n1之间的运算
if(b==0) {//n0+n3
if(c==0) {//n2+n1
sb.append("(").append(n[0]).append("+").append(n[3]).append(")").append(op).append("(").append(n[2]).append("+").append(n[1]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("+").append(n[3]).append(")").append(op).append("(").append(n[2]).append("-").append(n[1]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("+").append(n[3]).append(")").append(op).append("(").append(n[1]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("+").append(n[3]).append(")").append(op).append("(").append(n[2]).append("*").append(n[1]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("+").append(n[3]).append(")").append(op).append("(").append(n[2]).append("/").append(n[1]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("+").append(n[3]).append(")").append(op).append("(").append(n[1]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==1) {//n0-n3
if(c==0) {//n2+n1
sb.append("(").append(n[0]).append("-").append(n[3]).append(")").append(op).append("(").append(n[2]).append("+").append(n[1]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("-").append(n[3]).append(")").append(op).append("(").append(n[2]).append("-").append(n[1]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("-").append(n[3]).append(")").append(op).append("(").append(n[1]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("-").append(n[3]).append(")").append(op).append("(").append(n[2]).append("*").append(n[1]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("-").append(n[3]).append(")").append(op).append("(").append(n[2]).append("/").append(n[1]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("-").append(n[3]).append(")").append(op).append("(").append(n[1]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==2) {//n3-n0
if(c==0) {//n2+n1
sb.append("(").append(n[3]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("+").append(n[1]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[3]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("-").append(n[1]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[3]).append("-").append(n[0]).append(")").append(op).append("(").append(n[1]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[3]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("*").append(n[1]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[3]).append("-").append(n[0]).append(")").append(op).append("(").append(n[2]).append("/").append(n[1]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[3]).append("-").append(n[0]).append(")").append(op).append("(").append(n[1]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==3) {//n0*n3
if(c==0) {//n2+n1
sb.append("(").append(n[0]).append("*").append(n[3]).append(")").append(op).append("(").append(n[2]).append("+").append(n[1]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("*").append(n[3]).append(")").append(op).append("(").append(n[2]).append("-").append(n[1]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("*").append(n[3]).append(")").append(op).append("(").append(n[1]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("*").append(n[3]).append(")").append(op).append("(").append(n[2]).append("*").append(n[1]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("*").append(n[3]).append(")").append(op).append("(").append(n[2]).append("/").append(n[1]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("*").append(n[3]).append(")").append(op).append("(").append(n[1]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==4) {//n0/n3
if(c==0) {//n2+n1
sb.append("(").append(n[0]).append("/").append(n[3]).append(")").append(op).append("(").append(n[2]).append("+").append(n[1]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[0]).append("/").append(n[3]).append(")").append(op).append("(").append(n[2]).append("-").append(n[1]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[0]).append("/").append(n[3]).append(")").append(op).append("(").append(n[1]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[0]).append("/").append(n[3]).append(")").append(op).append("(").append(n[2]).append("*").append(n[1]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[0]).append("/").append(n[3]).append(")").append(op).append("(").append(n[2]).append("/").append(n[1]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[0]).append("/").append(n[3]).append(")").append(op).append("(").append(n[1]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}if(b==5) {//n3-n0
if(c==0) {//n2+n1
sb.append("(").append(n[3]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("+").append(n[1]).append(")").append("=24");
}if(c==1) {//n2-n3
sb.append("(").append(n[3]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("-").append(n[1]).append(")").append("=24");
}if(c==2) {//n3-n2
sb.append("(").append(n[3]).append("/").append(n[0]).append(")").append(op).append("(").append(n[1]).append("-").append(n[2]).append(")").append("=24");
}if(c==3) {// n2*n3
sb.append("(").append(n[3]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("*").append(n[1]).append(")").append("=24");
}if(c==4) {// n2/n3
sb.append("(").append(n[3]).append("/").append(n[0]).append(")").append(op).append("(").append(n[2]).append("/").append(n[1]).append(")").append("=24");
}if(c==5) {// n3/n2
sb.append("(").append(n[3]).append("/").append(n[0]).append(")").append(op).append("(").append(n[1]).append("/").append(n[2]).append(")").append("=24");
}
//将运算表达式存入容器
set.add(sb.toString());
//将sb清空
sb = new StringBuilder();
}
}
}
}

四、运行结果

1
2
3
4
5
6
7
四个数字为:9 Q A 8 
(12 -9 )*(1 *8 )=24
(12 -9 )*(8 /1 )=24
(12 -9 )/(1 /8 )=24

Process finished with exit code 0