N个数的所有组合

来源:百度知道 编辑:UC知道 时间:2024/05/09 06:24:47
组合数为2的N次方个

是求幂集吧?

#include <iostream>

using namespace std;

int c = 0;
void powerset(int a[], int n, int t, int b[]) {
if (t == n) {
for (int i = 0; i < n; i++)
if (b[i] == 1)
cout << a[i] << "\t";
cout << endl;c++;
} else {
for (int i = 0; i <= 1; i++) {
b[t] = i;
powerset(a, n, t+1, b);
}
}
}

int main() {
const int n = 6;
int a[n] = {0, 1, 2, 3, 4, 5};
int b[n];
powerset(a, n, 0, b);
cout << c << endl; //for testing
return 0;
}