11第k个排列
1 题目描述
2 代码
#include "iostream"
#include "vector"
using namespace std;
void dfs(vector<bool> &fg, int n, string path, string &res, int &count) {
if (path.size() == n) {
count--;
if (count == 0) {
res = path;
}
return ;
}
for (int i = 0; i < fg.size(); i++) {
if (!fg[i]) {
fg[i] = true;
dfs(fg, n, path+ to_string(i+1), res, count);
fg[i] = false;
if (count == 0) {
return;
}
}
}
}
int main() {
int n;
int id;
cin >> n >> id;
vector<bool> fg(n);
string res;
int count = id;
dfs(fg, n, "", res, count);
if (!res.empty()) {
cout << res << endl;
}
return 0;
}