標籤:style blog color os io ar for art div
遞迴,遞迴:
/*ID: qq104801LANG: C++TASK: zerosum*/#include <iostream>#include <fstream>#include <string>#include <vector>#include <cstdio>#include <algorithm>using namespace std;int N;void dfs(int n,int sum,int curr,vector<char> path){ if(n==N) { if(sum+curr==0) { for(int i=1;i<n;++i) cout<<i<<path[i]; cout<<N<<endl; } } else { const int next=n+1; path[n]=‘ ‘; dfs(n+1,sum,curr*10+(curr>0?next:-next),path); path[n]=‘+‘; dfs(n+1,sum+curr,next,path); path[n]=‘-‘; dfs(n+1,sum+curr,-next,path); }}void test(){ freopen("zerosum.in","r",stdin); freopen("zerosum.out","w",stdout); cin>>N; vector<char> start(N,0); dfs(1,0,1,start);}int main () { test(); return 0;}
test data:
USER: cn tom [qq104801]TASK: zerosumLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.011 secs, 3516 KB] Test 2: TEST OK [0.003 secs, 3516 KB] Test 3: TEST OK [0.003 secs, 3516 KB] Test 4: TEST OK [0.003 secs, 3516 KB] Test 5: TEST OK [0.003 secs, 3516 KB] Test 6: TEST OK [0.008 secs, 3516 KB] Test 7: TEST OK [0.011 secs, 3516 KB]All tests OK.YOUR PROGRAM (‘zerosum‘) WORKED FIRST TIME! That‘s fantastic -- and a rare thing. Please accept these special automated congratulations.Here are the test data inputs:------- test 1 ----3------- test 2 ----4------- test 3 ----5------- test 4 ----6------- test 5 ----7------- test 6 ----8------- test 7 ----9Keep up the good work!Thanks for your submission!
usaco-2.3-zerosum-pass