You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
#define str to_string //利用define缩短代码
vector<string> path;
const int N = 30;
int a[N];
void dfs(int step, int res) {
if (step == 4) {
if (res == 24) {
for (int i = 0; i < path.size(); i++, puts("")) printf("%d", path[i]);
exit(0);
}
return;
}
int x = a[step];
if (x > res) swap(x, res);
path.push_back(str(res) + "+" + str(x) + "=" + str(res + x));
dfs(step + 1, res + x);
path.pop_back();
path.push_back(str(res) + "-" + str(x) + "=" + str(res - x));
dfs(step + 1, res - x);
path.pop_back();
path.push_back(str(res) + "*" + str(x) + "=" + str(res * x));
dfs(step + 1, res * x);
path.pop_back();
if (x && res % x == 0) {
path.push_back(str(res) + "/" + str(x) + "=" + str(res / x));
dfs(step + 1, res / x);
path.pop_back();
}
}
int main() {
for (int i = 0; i < 4; i++) scanf("%d", &a[i]);
do {
dfs(1, a[0]);
} while (next_permutation(a, a + 4));
puts("No answer!");
return 0;
}
/*
**?**5
1.((a?b)?c)?d
2.(a?(b?c))?d
3.a?((b?c)?d)
4.(a?b)?(c?d)
5.a?(b?(c?d))
*/