首页 > 其他 > 详细

usaco-2.3-zerosum-pass

时间:2014-09-05 15:45:21      阅读:333      评论:0      收藏:0      [点我收藏+]

递归,递归:

/*
ID: qq104801
LANG: 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: zerosum
LANG: C++

Compiling...
Compile: OK

Executing...
   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 ----
9

Keep up the good work!
Thanks for your submission!

 

usaco-2.3-zerosum-pass

原文:http://www.cnblogs.com/dpblue/p/3958051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!