usaco-2.3-zerosum-pass

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.