39 lines
785 B
C++
39 lines
785 B
C++
#include <iostream>
|
||
#include<algorithm>
|
||
#include<vector>
|
||
using namespace std;
|
||
//算法:https://blog.csdn.net/wumuzi520/article/details/8087501#comments
|
||
void Combination(int arr[], int nLen, int m, int out[], int outLen)
|
||
{
|
||
if(m == 0)
|
||
{
|
||
for (int j = 0; j < outLen; j++)
|
||
cout << out[j] << "\t";
|
||
cout << endl;
|
||
|
||
return;
|
||
}
|
||
|
||
for (int i = nLen; i >= m; --i) //从后往前依次选定一个
|
||
{
|
||
out[m-1] = arr[i-1]; //选定一个后
|
||
Combination(arr,i-1,m-1,out,outLen); // 从前i-1个里面选取m-1个进行递归
|
||
}
|
||
}
|
||
void PrintCombination(int arr[], int nLen, int m)
|
||
{
|
||
if(m > nLen)
|
||
return;
|
||
|
||
int* out = new int[m];
|
||
Combination(arr,nLen,m,out,m);
|
||
delete [] out;
|
||
}
|
||
|
||
int main() {
|
||
int array[] = { 1,2,3,4,5,6};
|
||
PrintCombination(array,6,3);
|
||
return 0;
|
||
}
|
||
|