给你N个整数,分别是1,2,3,。。。N。问你全排列的第M个排列为多少?
Input
输入包含几个测试用例。每个测试用例由两个数字组成,N和M(1<=N<=1000, 1<=M<=10000). 你可能会假设总有一个序列满足题目需求。输入在文件结束时终止。
Output
对于每个测试用例,您只需要输出满足题目要求的序列。输出序列时,应该在两个数字之间打印空格,但不要在最后一个数字之后输出任何空格。
Sample Input
6 4 11 8
Sample Output
1 2 3 5 6 4 1 2 3 4 5 6 7 9 8 11 10
#include<iostream>
#include<algorithm>
using namespace std;
int s[1000000];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
for(i=0;i<n;i++)
{
s[i]=i+1;
}
int t=2;
while(next_permutation(s,s+n))
{
if(t==m)
{
for(int i=0;i<n-1;i++)
{
cout<<s[i]<<" ";
}
cout<<s[n-1]<<endl;
break;
}
t++;
}
}
return 0;
}