输出1~n的按字典序从小到大的顺序数第m个排列
#include <iostream>
using namespace std;
const int N = 110;
int n, m;
int path[N];
bool st[N];
bool dfs(int u)
{
if (u == n)
{
m--;
if (!m)
{
for (int i = 0; i < n; i++)
cout << path[i] << " ";
cout << endl;
return true;
}
return false;
}
for (int i = 1; i <= n; i++)
{
if (!st[i])
{
path[u] = i;
st[i] = true;
if (dfs(u + 1))
return true;
st[i] = false;
}
}
return false;
}
int main()
{
cin >> n >> m;
dfs(0);
return 0;
}