Renewed Structure
This commit is contained in:
51
Algorithm/Recursion/P29_25_SubsetGen.cpp
Normal file
51
Algorithm/Recursion/P29_25_SubsetGen.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user