C++ 代码
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> ret;
void dfs(int idx){
if(ret.size() + n - idx + 1 < m) return; //无法选m个就提前结束
if(idx == n + 1 && ret.size() < m) return;
if(ret.size() == m){
for(auto x : ret)
cout << x << " ";
cout << endl;
return;
}
ret.push_back(idx);
dfs(idx + 1); //此数选取
ret.pop_back();
dfs(idx + 1); //此数不选取
}
int main()
{
cin >> n >> m;
dfs(1);
return 0;
}