Ignatius and the Princess II

Ignatius and the Princess II

题目链接:Ignatius and the Princess II

题目概述:给出多行,每行有n和m两个数,n代表1,2,3…,n。n个数字可组合成不同的序列,求出第m小的序列。这里按大小比较是按字典顺序。用例子说明一下字典顺序,有a,b,c,d四个数,组成的由小到大的序列是:

a b c d
a b d c  //第三列,d在c后面
a c b d  //第二列,c在b后面
a c d b  //第二列,c在b后面,第三列,d在b后面
a d b c
a d c b
b a c d
....
....

字母换成数字即是该题中的大小序列。STL中next_permutation即实现了该算法,简单调用,设置count统计即可。

Input:

6 4
11 8

Output:

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

解题思路:


最终代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int a[10000],coun,n,m,i;
int main()
{
    while(cin>>n>>m)
    {
        for(i=0;i<n;i++)
        {
            a[i] = i+1;
        }
        coun = 1;
        while(next_permutation(a,a+n)
        {
            coun++;
            if(coun==m)
                break;
        }
        printf("%d",a[0]);
        for(i=1;i<n;i++)
            printf(" %d",a[i]);
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只放纵的死魂灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值