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); }
运行结果: