|
- #include<bits/stdc++.h>
- using namespace std;
- int n;
- char c[15];
- int sum;
- void dfs(int k,int ans,int p)//1+ 2-
- {
- if(k==n)
- {
- if(p==1) sum+=ans;
- if(p==2) sum-=ans;
- //cout<<sum<<endl;
- if(sum==0)
- {for(int i=1;i<n;i++) cout<<i<<c[i];
- cout<<n<<endl;}
- if(p==1) sum-=ans;
- if(p==2) sum+=ans;
- return;
- }
- else
- {
- if(p==1) {
- c[k]=' ';
- dfs(k+1,ans*10+k+1,1);
- sum+=ans;
- c[k]='+';
- dfs(k+1,k+1,1);
- c[k]='-';
- dfs(k+1,k+1,2);
- sum-=ans;
- }
- if(p==2)
- {
- c[k]=' ';
- dfs(k+1,ans*10+k+1,2);
- sum-=ans;
- c[k]='+';
- dfs(k+1,k+1,1);
- c[k]='-';
- dfs(k+1,k+1,2);
- sum+=ans;
- }
- }
- }
- int main()
- {
- cin>>n;
- dfs(1,1,1);
- return 0;
- }
复制代码 |
|