You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
647 B

#include <bits/stdc++.h>
using namespace std;
int n, r;
void dfs(int step, vector<int> path, int m) {
//过不了就减减枝吧,剩下的不够了,就别再继续了。
if (r - m > n - step) return;
if (step == n + 1) return;
if (m == r) {
for (int i = 0; i < r; i++) printf("%3d", path[i]);
printf("\n");
return;
}
//下一个数字选择上
vector<int> p1 = path;
p1.push_back(step + 1);
dfs(step + 1, p1, m + 1);
//下一个数字放弃掉
dfs(step + 1, path, m);
}
int main() {
scanf("%d%d", &n, &r);
dfs(0, {}, 0);
return 0;
}