c++ ACM:递归实例

#include <iostream>
using namespace std;
void backtrack(int a[] , int n , int k){
    if(k == n+1){
        for(int j = 1 ; j <= n ; j++) cout<<a[j];
        cout<<endl;
        return ;
    }
    a[k] = 0 ;
    backtrack(a, n, k+1);
    a[k] = 1 ;
    backtrack(a, n, k+1);
}
void main()
{
    int n;
    cin>>n;
    int* a = new int[n+1];
    backtrack(a,n,1);
}

运行结果:

 

 

posted @ 2013-03-06 23:31  KeenLeung  阅读(411)  评论(0编辑  收藏  举报