https://nanti.jisuanke.com/t/59
简单dfs+字符串检索
#include <iostream>
#include <cctype>
using namespace std;
int N, END;
int calc(string s)
{
int ans = 0, sum = 0;
int flag = 1;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') continue;
if (isdigit(s[i])) {
sum = sum*10 + s[i]-48;
} else if (s[i] == '+') {
ans += sum*flag;
flag = 1, sum = 0;
} else if (s[i] == '-') {
ans += sum*flag;
flag = -1, sum = 0;
}
}
ans += sum*flag;
return ans;
}
void dfs(string str, int cur)
{
if (cur > END) return;
if (cur == END) {
string s = "";
for (int i = 1; i <= N; i++) {
s += (i+48);
s += str[i-1];
}
int ans = calc(s);
if (ans == 0) cout << s << endl;
return;
}
dfs(str+" ", cur+1);
dfs(str+"+", cur+1);
dfs(str+"-", cur+1);
}
int main()
{
while (cin >> N) {
string str = "";
END = N - 1;
dfs(str, 0);
}
return 0;
}