悲催的题意 看了N久 最后还是让WL帮翻译的 原来空格的意思是多位数 比如2 3 就是23
dfs 枚举所有可能的组合 再保存为0的 再排下序就OK
/*
ID: your_id_here
PROG: zerosum
LANG: C++
*/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
char str[1010][20],s[20];
int n,t;
int cal(int a,int b,int c)
{
if(c=='+')
return a+b;
else
return a-b;
}
void dfs(int x)
{
int i,j,y[20],g=0;
s[2*x-2] = x+'0';
if(x==n)
{
s[2*x-1] = '\0';
int s1 = 0,k = 2*x-1;
for(i = 0 ; i < k; i++)
{
if(s[i]==' ')
{
y[g] = y[g]*10+s[i+1]-'0';
i++;
}
else
{
g++;
if(s[i]>='0'&&s[i]<='9')
y[g] = s[i]-'0';
else
y[g] = s[i];
}
}
s1 = y[1];
for(i = 3 ; i <= g ; i++)
{
if(i%2!=0)
s1 = cal(s1,y[i],y[i-1]);
}
if(s1==0)
{
strcpy(str[t],s);
t++;
}
return ;
}
s[2*x-1] = '+';
dfs(x+1);
s[2*x-1] = '-';
dfs(x+1);
s[2*x-1] = ' ';
dfs(x+1);
}
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
int main()
{
freopen("zerosum.in","r",stdin);
freopen("zerosum.out","w",stdout);
int i;
cin>>n;
dfs(1);
qsort(str,t,sizeof(str[0]),cmp);
for(i = 0 ; i < t ; i++)
puts(str[i]);
fclose(stdin);
fclose(stdout);
return 0;
}