hdu1027

 

题目大意:

先说这个题的大意,

给定一个数m=8;数列1 2 3 4 5 6 7 8 ;

全排列指的是第二大数列就是1 2 3 4 5 6 8 7,

第三大数列就是1 2 3 4 5 7 6 8,

第四大数列就是1 2 3 4 5 7 8 6,

第五大数列就是1 2 3 4 5 8 6 7,

七大1 2 3 4 5 8 7 6,

八1 2 3 4 6 5 7 8

输入m确定位数,输入n,

输出第n大的数列;

 

样列输入  m=6   n=4;

m=11  n=8;

样例输出:

1 2 3 5 6 4;

1 2 3 4 5 6 7 9 8 11 10;

本题解题思路:

这个题有个巧点,虽然n<=1000, 但m<=10000,所以n只算前8个数字就满足题意,那么全排列的总数在5040~40320之间,分为8个大段1,2,6,24,120,720,5040,40320,首先判断n的大小,得出分段区,变动几位数字,然后把n除以比小一个分段得到商,这个商就是当前位应该使用的第几大数字,再将商取余,余数赋值给n,,重复上一步操作,直到余数为0;

Problem Description

Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."

"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?

 

 

Input

The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.

 

 

Output

For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.

 

 

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

Author

Ignatius.L

Recommend

首先把题看懂,这个题有个巧点,虽然n<=1000, 但m<=10000,所以n

只算前8个就满足题意, 直接上代码,会详解;

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int biao[9]= {1,1,2,6,24,120,720,5040,40320};//这一步是把8个节点拿出来,待会好比较,这个要求出来下面有代码
int main()
{
    int n,m,x,a,cnt,i;
    int rest[2],store[9];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i = 0; i < 9; i++)
            store[i] = 1005;//建立的新数组,全为1005

        for(i = 1; i <= 8; i++)
            if(biao[i] > m)//比较m在哪个位置,用x来记录
            {
                x = i;
                break;
            }
        for(i = 1; i <= n - x; i++)
            printf("%d ",i);//将前面的无影响数字打出来
        for(i = 1; i <= x; i++)
            store[i] = n - x + i;//将后面要排序的数字放在store[]的前几个元素,通常是x个后面数字n,n-1,n-2......;
        for(i = x - 1; i >= 1; i--)
        {
            sort(store, store + 9);//对里面的数字进行排序,所有1005被放在后面
            a = m / biao[i];//核心:判断m中有几个biao[i],用来判断这个位置该放第几大的数字
            m %= biao[i];//将m取余,好用做下一判断是第几大数字
			if(m)//最后m等于0,或1;
            {
                printf("%d ",store[a]);
                store[a] = 1005;
            }
            else//这一步是m==0时,m是阶乘的倍数
            {
                printf("%d ",store[a - 1]);
                store[a - 1] = 1005;
                break;
            }
        }
        sort(store, store + 9);//这几步很重要,是为了防止break后,后面的数字还没打上去,添上剩下的数字
        for(i = 8; i > 0; i--)
            if(store[i] != 1005)
            printf("%d ",store[i]);
        printf("%d\n",store[0]);//格式问题
    }
	return 0;
}//注意体会,最好在纸上演算一遍

 

 

 

int stratum(int x)
{
    if(x==1)return 1;
    else
        return stratum(x-1)*x;
}//阶乘函数
int sort(int a[])
{
    int i,j,k,t;
    for(i=0;i<9;i++)
    {
        k=i;
        for(j=i+1;j<9;j++)
            if(a[k]>a[j])k=j;
        t=a[k];
        a[k]=a[j];
        a[j]=t;
    }
    return 0;
}//c语言sort

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值