前n个数,输出所有的集合:
例如前3个数有7种,前4个数有15种,以下当n=3时:
1
2
3
12
13
23
123
#include<iostream>
using namespace std;
const int N = 100;
bool st[N];
int n;
int size = 0;
void dfs(int x)
{
if(x == n + 1)
{
for(int i = 1; i <= n; i++)
{
if(st[i]) cout << i;
}
if(size!=0)cout << endl;
return;
}
st[x] = true;
size++;
dfs(x + 1);
size--;
st[x] = false;
dfs(x + 1);
}
int main() {
cin >> n;
dfs(1);
return 0;
}