从1到n的n个连续的整数中任取m个,求所有的排列组合,要求逆字典序输出
#include<iostream>
using namespace std;
void combine(int* s, int* r, int n)
{
//n=1时,输出结果
if (n == 1) {
while(*s) {
for (int j = 0; r[j] != '\0'; j++) {
cout << r[j];
}
cout << *s++ << endl;
}
}
//n不为1时:
else {
int i = 0, j = 0;
for (i = 0; s[i] != '\0'; i++);
for (j = 0; r[j] != '\0'; j++);
for (; i >= n; i--) {
r[j] = *s++;
r[j + 1] = '\0';
combine(s, r, n - 1);
}
}
}
int main()
{
int m = 0, n = 0;
cin >> n >> m;
if (m > n)
{
cout << "The input is error" << endl;
return 0;
}
int* source = new int[n];
int* result = new int[n];
memset(result,'\0',sizeof(result));
for (int i = 0; i < n; i++)
source[i] = n - i;
source[n] = '\0';
combine(source,result,m);
system("pause");
return 0;
}