Skip to content

Commit ead1834

Browse files
authored
Add files via upload
1 parent ae6b335 commit ead1834

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

24-game.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int raw[4], pl[4], oper[3], cnt = 0; //oper:0 addition, 1 subtraction, 2 multiplication, 3 division
5+
bool visit[13];
6+
7+
void dfs(int t);
8+
void operdfs(int t);
9+
void processoper();
10+
11+
int main() {
12+
for (int i = 0; i < 4; i++) cin >> raw[i];
13+
dfs(0);
14+
return 0;
15+
}
16+
17+
void dfs(int t) {
18+
if (t == 4) {
19+
operdfs(0);
20+
return;
21+
}
22+
23+
for (int i = 0; i < 4; i++) {
24+
if (!visit[raw[i]]) {
25+
pl[t] = raw[i]; visit[raw[i]] = true;
26+
dfs(t + 1);
27+
visit[raw[i]] = false;
28+
}
29+
}
30+
}
31+
32+
void operdfs(int t) { //t0=0
33+
if (t == 3) {
34+
processoper();
35+
return;
36+
}
37+
for (int i = 0; i < 4; i++) {
38+
oper[t] = i;
39+
operdfs(t + 1);
40+
}
41+
}
42+
43+
void processoper() {
44+
int res = pl[0];
45+
for (int i = 0; i < 3; i++) {
46+
switch (oper[i]) {
47+
case 0: res += pl[i + 1]; break;
48+
case 1: res -= pl[i + 1]; break;
49+
case 2: res *= pl[i + 1]; break;
50+
case 3: res /= pl[i + 1]; break;
51+
}
52+
}
53+
if (res == 24) {
54+
for (int i = 0; i < 3; i++) {
55+
cout << pl[i] << " ";
56+
switch (oper[i]) {
57+
case 0: cout << "+ "; break;
58+
case 1: cout << "- "; break;
59+
case 2: cout << "* "; break;
60+
case 3: cout << "/ "; break;
61+
}
62+
}
63+
cout << pl[3] << endl;
64+
}
65+
return;
66+
}

0 commit comments

Comments
 (0)