题目描述
指数型枚举
样例
3
1 2 3
1 2
1 3
1
2 3
2
3
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n;
//st=0没考虑,st=1选,st=2不选 为什么有三种状态呢,因为可以重复选,所以多一个状态st=0
int st[N];
void dfs(int x, int start) {
if(x > n) {
for(int i = 1; i <= n; i++) {
if(st[i] == 1) {
cout << i << " ";
}
}
cout << "\n";
return;
}
st[x] = 1;
dfs(x + 1, start + 1);
st[x] = 0;
//恢复现场
st[x] = 2;
dfs(x + 1, start + 1);
st[x] = 0;
}
int main() {
cin >> n;
dfs(1, 1);
return 0;
}