24点运算器

自动实现 24 点运算。

程序实现

简单暴力了所有可能的运算。

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
#include<bits/stdc++.h>
using namespace std;

void solve(){
vector<double>a(4);
for(auto &i : a) cin >> i;
for(int i = 0; i < 4; i++){
for(int j = i + 1; j < 4; j++){
vector<double>res1;
res1.emplace_back(a[i] + a[j]);
res1.emplace_back(a[i] - a[j]);
res1.emplace_back(a[j] - a[i]);
res1.emplace_back(a[i] * a[j]);
res1.emplace_back(a[i] / a[j]);
res1.emplace_back(a[j] / a[i]);

// res1 与 剩下的其中一个数 先运算
{
for(int u = 0; u < 4; u++){
if(u == i || u == j) continue;
for(int k = 0; k < 6; k++){
vector<double>res2;
res2.emplace_back(res1[k] + a[u]);
res2.emplace_back(res1[k] - a[u]);
res2.emplace_back(a[u] - res1[k]);
res2.emplace_back(res1[k] * a[u]);
res2.emplace_back(res1[k] / a[u]);
res2.emplace_back(a[u] / res1[k]);

for(int v = 0; v < 4; v++){
if(v == i || v == j || v == u) continue;
for(int h = 0; h < 6; h++){
vector<double>res3;
res3.emplace_back(res2[h] + a[v]);
res3.emplace_back(res2[h] - a[v]);
res3.emplace_back(a[v] - res2[h]);
res3.emplace_back(res2[h] * a[v]);
res3.emplace_back(res2[h] / a[v]);
res3.emplace_back(a[v] / res2[h]);

for(int g = 0; g < 6; g++){
if(fabs(res3[g] - 24) < 1e-4){

// auto show_op = [](double x, double y, int op){
// if(op == 0) cout << x << " + " << y << " = " << x + y << '\n';
// if(op == 1) cout << x << " - " << y << " = " << x - y << '\n';
// if(op == 2) cout << y << " - " << x << " = " << y - x << '\n';
// if(op == 3) cout << x << " * " << y << " = " << x * y << '\n';
// if(op == 4) cout << x << " / " << y << " = " << x / y << '\n';
// if(op == 5) cout << y << " / " << x << " = " << y / x << '\n';
// };

// show_op(a[i], a[j], k);
// show_op(res1[k], a[u], h);
// show_op(res2[h], a[v], g);

auto show_op = [](string x, string y, int op) -> string{
string res;
res += "(";
if(op == 0) res += x + " + " + y;
if(op == 1) res += x + " - " + y;
if(op == 2) res += y + " - " + x;
if(op == 3) res += x + " * " + y;
if(op == 4) res += x + " / " + y;
if(op == 5) res += y + " / " + x;
res += ")";
return res;
};

string o1 = to_string((int)a[i]), o2 = to_string((int)a[j]),
o3 = to_string((int)a[u]), o4 = to_string((int)a[v]);

cout << show_op(show_op(show_op(o1, o2, k), o3, h), o4, g) << '\n';
return;
}
}
}
}
}
}
}

// 剩下的两个数先运算
{
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;
vector<double>res2;
res2.emplace_back(a[u] + a[v]);
res2.emplace_back(a[u] - a[v]);
res2.emplace_back(a[v] - a[u]);
res2.emplace_back(a[u] * a[v]);
res2.emplace_back(a[u] / a[v]);
res2.emplace_back(a[v] / a[u]);

for(int k = 0; k < 6; k++){
for(int h = 0; h < 6; h++){
vector<double>res3;
res3.emplace_back(res1[k] + res2[h]);
res3.emplace_back(res1[k] - res2[h]);
res3.emplace_back(res2[h] - res1[k]);
res3.emplace_back(res1[k] * res2[h]);
res3.emplace_back(res1[k] / res2[h]);
res3.emplace_back(res2[h] / res1[k]);

for(int g = 0; g < 6; g++){
if(fabs(res3[g] - 24) < 1e-4){

// auto show_op = [](double x, double y, int op){
// if(op == 0) cout << x << " + " << y << " = " << x + y << '\n';
// if(op == 1) cout << x << " - " << y << " = " << x - y << '\n';
// if(op == 2) cout << y << " - " << x << " = " << y - x << '\n';
// if(op == 3) cout << x << " * " << y << " = " << x * y << '\n';
// if(op == 4) cout << x << " / " << y << " = " << x / y << '\n';
// if(op == 5) cout << y << " / " << x << " = " << y / x << '\n';
// };

// show_op(a[i], a[j], k);
// show_op(a[u], a[v], h);
// show_op(res1[k], res2[h], g);

auto show_op = [](string x, string y, int op) -> string{
string res;
res += "(";
if(op == 0) res += x + " + " + y;
if(op == 1) res += x + " - " + y;
if(op == 2) res += y + " - " + x;
if(op == 3) res += x + " * " + y;
if(op == 4) res += x + " / " + y;
if(op == 5) res += y + " / " + x;
res += ")";
return res;
};

string o1 = to_string((int)a[i]), o2 = to_string((int)a[j]),
o3 = to_string((int)a[u]), o4 = to_string((int)a[v]);

cout << show_op(show_op(o1, o2, k), show_op(o3, o4, h), g) << '\n';
return;
}
}
}
}
}
}
}
}
}
cout << -1 << '\n';
}

signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}

运行展示

可见对一些需要分数运算的复杂24点也是可以支持的。

附一个可以多测的 .exe 文件:play_24points.exe. ( 点 Raw 即可下载)

more

程序都写出来了顺便测测:当每个数从 1~10 随机选取时(大家最常玩的情况),有多大概率可以凑成24点。

运行结果:可以凑成的情况有8558种,可行率高达 8558/10000 = 85.58%.

看来 24 这个数字选的还是很有讲究的。

upd

更多想法放在这里了:2025年啦,还在用”4”个数算”24点”嘛? - 知乎,欢迎浏览。