该说终于rand到一道正常画风的搜索题吗...
本题就是简单dfs 不过在“.”这个符号的处理上边要想想...
注意深度大于20层是不打印的...之前因为这个OLE了好几次...
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n, tot;
char opera[20];
void dfs(int depth, int sum, int pre) {
if(depth == n) {
if(!sum) {
tot++;
if(tot <= 20) {
for(int i = 1; i < n; ++i) printf("%d %c ", i, opera[i]);
printf("%d\n", n);
}
}
}
else {
opera[depth] = '+';
dfs(depth + 1, sum + depth + 1, depth + 1);
opera[depth] = '-';
dfs(depth + 1, sum - depth - 1, depth + 1);
opera[depth] = '.'; int next;
if(depth < 9) next = pre * 10 + depth + 1;
else next = pre * 100 + depth + 1;
int i = depth - 1;
while(opera[i] == '.' && ~i) --i;
if(opera[i] == '+') dfs(depth + 1, sum + next - pre, next);
else if(opera[i] == '-') dfs(depth + 1, sum - next + pre, next);
}
}
int main() {
scanf("%d", &n);
opera[0] = '+';
dfs(1, 1, 1);
printf("%d\n", tot);
return 0;
}