直接DFS,因为实在没想到什么剪枝了...
注意一点是,10.11使用的是1011哦
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL __int64
using namespace std;
int n,ct,k;
char str[20];
void dfs(LL sum,LL last,int pos){
if(pos==n+1){
if(sum==0){
ct++;
if(ct<=20){
for(int i=1;i<n;i++){
printf("%d %c ",i,str[i]);
}
printf("%d\n",n);
}
}
return ;
}
str[pos-1]=‘+‘;
dfs(sum+pos,pos,pos+1);
str[pos-1]=‘-‘;
dfs(sum-pos,-pos,pos+1);
str[pos-1]=‘.‘;
if(pos>=10){
k=100;
}
else k=10;
if(last<0)
dfs(sum-last+last*k-pos,last*k-pos,pos+1);
else if(last>0){
dfs(sum-last+last*k+pos,last*k+pos,pos+1);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
ct=0;
dfs(1,1,2); //sum,last,pos,char
printf("%d\n",ct);
}
return 0;
}
原文:http://www.cnblogs.com/jie-dcai/p/4290998.html