递归方法打印子集
输入:n(<100)
输出:{0,1,2···n-1}集合的子集 的子集
如:
输入;3
输出:
0
0 1
0 1 2
0 2
1
1 2
2
#include <iostream>
using namespace std;
long long counts = 0;
void df(int leve, int a[], int n) {
for (int i = 0; i < n; i++) {
if (i > a[leve - 1]) {
a[leve] = i;
counts++;
for (int j = 1; j <= leve; j++) //输出当前子集合
{
cout << a[j] << " ";
}
cout << endl;
df(leve + 1, a, n);
}
}
}
int main() {
int n;
cin >> n;
int a[100];
a[0] = -1;
df(1, a, n);
// cout << counts << endl;
return 0;
}