洛谷P1157----组合数的输出
#include<stdio.h> int a[21] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; int result[21]; int n, r; int j = 0; void dfs(int x) {//x表示当前是第几个数 int ii; if (x > r) { for (ii = 0; ii < r; ii++) { printf("%3d", result[ii]); } printf("\n"); return; } for (ii = x; ii <=n-r+x; ii++) { if (a[ii] == 0&&(x==1||ii>result[x-2])) {//每个数都必须比他的前一个数大 a[ii] = 1; result[x-1] = ii; dfs(x + 1); a[ii] = 0; } } } int main() { scanf("%d %d", &n, &r); dfs(1); return 0; }