"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
6 4 11 8
1 2 3 5 6 4 1 2 3 4 5 6 7 9 8 11 10
这是一道全排列的题!可以调用库函数!next_permuatio(数组起点,数组终点)!按字典序排列!
全排列next_permutation 转载
这个函数可以计算一组数据的全排列
假设数列 d1,d2,d3,d4……
范围由[first,last)标记,调用next_permutation使数列逐次增大,这个递增过程按照字典序。
若当前调用排列到达最大字典序,比如dcba,就返回false,同时重新设置该排列为最小字典序。
返回为true表示生成下一排列成功。
另外,库中另一函数prev_permutation与next_permutation相反,由原排列得到字典序中上一次最近排列。
**********************************************************************************************
#include<vector>
#include<algorithm>
using namespace std;
{
while (cin>>n>>m)
{
for (i=0;i<n;i++)
{
a[i] = i+1;
}
vector<int> iv(a,a+n);
coun = 1;
{
coun++;
if (coun == m)
break;
}
printf("%d",iv[0]);
for (i = 1; i < n; ++i)
printf(" %d",iv[i]);
printf("/n");
}
return 0;
}
#include<stdio.h>
int a[1001];
int s[8];
{
int i,j,k;
int h,n,m,temp;
for (i = 1; i <= 1000; i++)
a[i] = i;
s[1] = 1; s[2] = 2; s[3] = 3; s[4] = 7;
s[5] = 25; s[6] = 121; s[7] = 721; s[8] = 5041;
while (scanf("%d%d",&n,&m)!=EOF)
{
while (m != 1)
{
for (i = 8; i > 0; i--)
if (m >= s[i])
break;
m = m-s[i]+1;
for (j = n-i+2; j <= n; j++)
{
for (k = n; k > j; k--)
if (a[k] < a[k-1])
{
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
}
}
h = n-i+2;
while (a[n-i+1] > a[h])
h++;
temp = a[n-i+1];
a[n-i+1] = a[h];
a[h] = temp;
}
for(j = 1; j <= n; j++)
printf(j-1?" %d":"%d",a[j]);
printf("/n");
for(j = 1; j <= n; j++)
a[j] = j;
}
return 1;
}