题目描述:

题目大意:现在给我们两个数字,N和M。我们应该编程找出由1到N组成的第M个最小序列。主要运用了全排列的思想,运用了全排列中next_permutation()函数;

next_permutation 使用方法:next_permutation(数组头地址,数组尾地址);若下一个排列存在,则返回真,如果不存在则返回假

举例:

对于数组a={1,2,3};

do
{
  cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
} while (next_permutation(a,a+3));//参数3指的是要进行排列的长度

输出的是:

1 2 3
 1 3 2
 2 1 3
 2 3 1
 3 1 2
 3 2 1

如果改成 while(next_permutation(a,a+2)),则输出:
 1 2 3
 2 1 3
只对前两个元素进行字典排序,显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
 代码实现:

#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int a[];
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
a[i]=i+;
for(int i=;i<m;i++)
{
next_permutation(a,a+n);//每执行一次,就重新排序一次
if(i==m-)//执行m-1次后输出结果
{
for(int j=;j<n-;j++)
printf("%d ",a[j]);
printf("%d\n",a[n-]);
}
}
}
return ;
}

全排列-hdu1027的更多相关文章

  1. hdu1027 Ignatius and the Princess II (全排列 &amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

  2. HDU1027 Ignatius and the Princess II( 逆康托展开 )

    链接:传送门 题意:给出一个 n ,求 1 - n 全排列的第 m 个排列情况 思路:经典逆康托展开,需要注意的时要在原来逆康托展开的模板上改动一些地方. 分析:已知 1 <= M <= ...

  3. PHP实现全排列(递归算法)

    算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为:    ① 如果n=1,则排列P只有一 ...

  4. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

  5. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  6. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  7. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. 全排列算法的JS实现

    问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...

随机推荐

  1. Activity Window View WindowManager关系&Touch事件分发机制

    http://www.cnblogs.com/linjzong/p/4191891.html https://www.cnblogs.com/kest/p/5141817.html https://b ...

  2. Activity,Fragment的状态保存

    http://blog.csdn.net/hqdoremi/article/details/26376797 https://blog.csdn.net/u013588712/article/deta ...

  3. SILC超像素分割算法详解(附Python代码)

    SILC算法详解 一.原理介绍 SLIC算法是simple linear iterative cluster的简称,该算法用来生成超像素(superpixel) 算法步骤: 已知一副图像大小M*N,可 ...

  4. 攻击者利用的Windows命令、横向渗透工具分析结果列表

    横向渗透工具分析结果列表 https://jpcertcc.github.io/ToolAnalysisResultSheet/ 攻击者利用的Windows命令 https://blogs.jpcer ...

  5. 【vim】保存文件没有权限 :w !sudo tee %

    每当你打开一个你没有写入权限的文件(比如系统配置文件)并做了一些修改,Vim 无法通过普通的 ":w" 命令来保存. 你不需要重新以 root 方式打开文件再进行修改,只需要运行: ...

  6. 使用 Linux 系统调用的内核命令【转】

    转自:http://www.ibm.com/developerworks/cn/linux/l-system-calls/ 探究 SCI 并添加自己的调用 Linux® 系统调用 —— 我们每天都在使 ...

  7. aiohttp分流处理

    # -*- coding: utf-8 -*- # @Time : 2018/12/26 9:55 PM # @Author : cxa # @Software: PyCharm import asy ...

  8. jvm系列二、JVM内存结构

    原文链接:http://www.cnblogs.com/ityouknow/p/5610232.html 所有的Java开发人员可能会遇到这样的困惑?我该为堆内存设置多大空间呢?OutOfMemory ...

  9. 读SRE Google运维解密有感(一)

    前言 这几天打算利用碎片时间读了一下"SRE Google运维解密"这本书,目前读了前几章,感觉收获颇多,结合自己的工作经历和书中的要点,写一些感悟和思考 SRE 有关SRE我就不 ...

  10. 一个shell的面试题

    5.写一个脚本,实现判断192.168.1.024网络里,当前在线的D有哪些,能ping通则认为在线,在线输出"TP地址UP",不在线输出TP地址DOWN",无其他输出. ...