前t个组合结果
#include <iostream> #include <cstdio> using namespace std; int X[31], used[31]; int n, r, t, count; void output() { for (int i = 1; i <= r; ++i)//输出r个 { printf(" %d", X[i]); } putchar('\n'); } int pruning(int i) { if (used[i]) return 0; return 1; } void f(int k, int i) { if (t == count) return ; if (k - 1 == r)//只组合r个 { ++count; output(); } else { for (;i >= 1; --i) { if (pruning(i)) { X[k] = i; used[i] = 1; f(k + 1, i - 1); used[i] = 0; } } } } int main() { scanf("%d %d %d", &n, &r, &t); f(1, n); return 0; }========================================Talk is cheap, show me the code=======================================
CSDN博客地址:https://blog.csdn.net/qq_34115899