Skip to content

Commit 9c3efa3

Browse files
Make Peter's code human-readable
1 parent 45d17c6 commit 9c3efa3

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

24-solver.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
11
#include <iostream>
22
using namespace std;
33

4-
int raw[4], pl[4], oper[3], cnt = 0; //oper:0 addition, 1 subtraction, 2 multiplication, 3 division
4+
int input[4], process[4], operator[3], cnt = 0; // Operators: 0 - add, 1 - subtract, 2 - multiply, 3 - divide
55
bool visit[13];
66

7-
void dfs(int t);
8-
void operdfs(int t);
9-
void processoper();
7+
void number_dfs(int t);
8+
void operator_dfs(int t);
9+
void caculate();
1010

1111
int main() {
12-
for (int i = 0; i < 4; i++) cin >> raw[i];
13-
dfs(0);
12+
for (int i = 0; i < 4; i++) cin >> input[i];
13+
number_dfs(0);
1414
return 0;
1515
}
1616

17-
void dfs(int t) {
17+
void number_dfs(int t) {
1818
if (t == 4) {
19-
operdfs(0);
19+
operator_dfs(0);
2020
return;
2121
}
2222

2323
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;
24+
if (!visit[input[i]]) {
25+
process[t] = input[i]; visit[input[i]] = true;
26+
number_dfs(t + 1);
27+
visit[input[i]] = false;
2828
}
2929
}
3030
}
3131

32-
void operdfs(int t) { //t0=0
32+
void operator_dfs(int t) { //t0=0
3333
if (t == 3) {
34-
processoper();
34+
caculate();
3535
return;
3636
}
3737
for (int i = 0; i < 4; i++) {
38-
oper[t] = i;
39-
operdfs(t + 1);
38+
operator[t] = i;
39+
operator_dfs(t + 1);
4040
}
4141
}
4242

43-
void processoper() {
44-
int res = pl[0];
43+
void caculate() {
44+
int res = process[0];
4545
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;
46+
switch (operator[i]) {
47+
case 0: res += process[i + 1]; break;
48+
case 1: res -= process[i + 1]; break;
49+
case 2: res *= process[i + 1]; break;
50+
case 3: res /= process[i + 1]; break;
5151
}
5252
}
5353
if (res == 24) {
5454
for (int i = 0; i < 3; i++) {
55-
cout << pl[i] << " ";
56-
switch (oper[i]) {
55+
cout << process[i] << " ";
56+
switch (operator[i]) {
5757
case 0: cout << "+ "; break;
5858
case 1: cout << "- "; break;
5959
case 2: cout << "* "; break;
6060
case 3: cout << "/ "; break;
6161
}
6262
}
63-
cout << pl[3] << endl;
63+
cout << process[3] << endl;
6464
}
6565
return;
6666
}

0 commit comments

Comments
 (0)