CH0301 递归实现指数型枚举

题目链接http://contest-hunter.org:83/contest/0x00%E3%80%8C%E5%9F%BA%E6%9C%AC%E7%AE%97%E6%B3%95%E3%80%8D%E4%BE%8B%E9%A2%98/0301%20%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%E6%8C%87%E6%95%B0%E5%9E%8B%E6%9E%9A%E4%B8%BE

时间复杂度度 O(2^n)

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 10;
const int inf = 0x7fffffff;
vector<int>chose;
int n;
void calc(int x){

    if (x == n + 1){  //问题边界
        for (int i = 0; i < chose.size(); i++)
            printf("%d ",chose[i]);
        cout << endl;
        return;
    }
    //"不选x"的分支
    calc(x + 1);  //子问题
    //"选x"的分支
    chose.push_back(x);  //记录x已被选择
    calc(x + 1);  //子问题
    chose.pop_back();    //准备回溯到上一问题之前,还原现场
}
int main()
{
    cin >> n;
    calc(1);
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/looeyWei/p/10529427.html