Files
Data-Structure/Algorithm/Recursion/P29_25_SubsetGen.cpp
2025-08-28 21:17:28 +08:00

52 lines
741 B
C++

//Generate subset
#include <iostream>
#include <cmath>
using namespace std;
//m: 本次递归选原数组第几个
//n: 本次递归数组长度
//l: 给定数组长度
template <class T>
T* sub(int m ,int n, int l, T* x, T* tmp){
if(m == l)
{
cout << "[ ";
for(int i = 0; i < n; i++){
cout << tmp[i] << " ";
}
cout << " ]" << endl;
return tmp;
}
//不选a[i]
sub(m + 1, n, l, x, tmp);
//选a[i]
tmp[n] = x[m];
sub(m + 1, n + 1, l, x, tmp);
}
int main(){
//cin >> n;
int n = 5;
int num = pow(2 ,n);
double** p;
p = new double* [num];
for(int i = 0; i < num ; i++){
p[i] = new double[n];
}
double* tmp = new double[n];
double arr[] = {1, 2, 3, 4, 5};
sub(0, 0, 5, arr, tmp);
return 0;
}