代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int n,m; int vis[maxn]; int mars[maxn]; int ans[maxn]; int num = 0; bool return0 = false;//直接结束 void dfs(int x) { if (return0)//直接结束 { return; } if (x > n) { num++; if (num == m + 1) { return0 = true;//直接结束 for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } } } for (int i = 1; i <= n; i++) { if (num == 0) { i = mars[x];//每个i只排列>mars[i]的数字 } if (vis[i] == 0) { vis[i] = 1; ans[x] = i; dfs(x + 1);//位置向后移动 vis[i] = 0; } } } int main() // 组合问题 { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> mars[i]; } dfs(1); }