按字典序排列,只要先选先输出就好
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
vector<int> choose;
void calc(int x)
{
if(n - x + 1 < m - choose.size()) return ;
if(m == choose.size())
{
for(int i = 0; i < m; i++)
printf("%d ",choose[i]);
puts("");
return ;
}
if(x == n + 1) return ;
//选
choose.push_back(x);
calc(x + 1);
choose.pop_back();//准备回溯到上一问题之前,还原现场。
//不选
calc(x + 1);
}
int main()
{
cin>>n>>m;
calc(1);
}