/**//*
ID: lorelei3
TASK: zerosum
LANG: C++
*/
#include <fstream>
#include <iostream>
using namespace std;
const int num[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
const char op[3] = {' ', '+' , '-'};
char ans[8];
int n,N;
ifstream fin;
ofstream fout;
int solve(){
int sum = 0, last=1, flag=1;
for(int i=0, j=1; i<n-1; ++i, ++j)
if(ans[i] == op[1]){
sum += last;
flag = 1;
last = num[j];
}else if(ans[i] == op[2]){
sum += last;
flag = -1;
last = -num[j];
}else if(ans[i] == op[0]){
last *=10;
last += flag*num[j];
}
sum += last;
return sum;
}
void output(){
fout<<1;
for(int i=0; i<n-1; ++i){
fout<<ans[i]<<num[i+1];
}
fout<<endl;
}
void dfs(int k){
if(k==n-1){
int sum = solve();
if(sum==0){
// solve();
output();
}
}else
for(int i=0; i<3; ++i){
ans[k] = op[i];
dfs(k+1);
}
}
int main(){
fin.open("zerosum.in");
fout.open("zerosum.out");
fin>>n;
dfs(0);
return 0;
}
posted on 2010-12-21 10:18
小阮 阅读(386)
评论(0) 编辑 收藏 引用 所属分类:
USACO