题目链接
http://noi-test.zzstep.com/contest/0x00%E3%80%8C%E5%9F%BA%E6%9C%AC%E7%AE%97%E6%B3%95%E3%80%8D%E4%BE%8B%E9%A2%98/0302%20%E9%80%92%E5%BD%92%EF%BC%8F%E9%9D%9E%E9%80%92%E5%BD%92%E5%AE%9E%E7%8E%B0%E7%BB%84%E5%90%88%E5%9E%8B%E6%9E%9A%E4%B8%BE
分析
相比指数型枚举,加上限制选出数个数的剪枝
AC代码
#include <cstdio>
const int maxn = 30;
int n, m, num[maxn], tot;
void dfs(int i) {
if (tot > m || tot + n - i + 1 < m) return;
if (i == n + 1) {
for (int i = 1; i <= tot; ++i) printf("%d ", num[i]);
puts("");
return;
}
num[++tot] = i;
dfs(i + 1);
--tot;
dfs(i + 1);
}
int main() {
scanf("%d%d", &n, &m);
dfs(1);
return 0;
}