全排列-hdu1027
题目描述:
题目大意:现在给我们两个数字,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的更多相关文章
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
- HDU1027 Ignatius and the Princess II( 逆康托展开 )
链接:传送门 题意:给出一个 n ,求 1 - n 全排列的第 m 个排列情况 思路:经典逆康托展开,需要注意的时要在原来逆康托展开的模板上改动一些地方. 分析:已知 1 <= M <= ...
- PHP实现全排列(递归算法)
算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为: ① 如果n=1,则排列P只有一 ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 全排列算法的JS实现
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...
随机推荐
- Ansi与Unicode编码
视频教程:Ansi与Unicode编码 大家在编程时经常遇到的数据类型: ● Ansi: char 代表一个字符 (CHAR) char * 代表一个字符串指针 (PCHAR PST ...
- JavaScript之HTML5 data-* 自定义属性[HTML5标准 node.dataset.attributeName]
在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,使用这样的结构可以进行数据存放. 使用data-*可以解决自定义属性混乱无管理的现状. ...
- Linux之Ubuntu与Windows更改默认启动顺序[转载]
装双系统后,经常会遇到与Windows更改默认启动顺序的需要,这样有助于开机时可以避免手动选择经常使用的系统了. 当然这解决办法不是博主的主意,本文纯属抄録者,故文章题目也声明了是转载,解决方案如下叙 ...
- MR数据生成工具指向目录
mrDataTidy_SaveTwoDays.jar 原始路径 :D:\太原MR数据\一天数据整理 目标路径 : D:\MR现场数据整理\保存两天_整理后数据 例如 当前时间:2017-5-17 10 ...
- 字符串cookies转字典 scrapy使用。
配置文件 DOWNLOADER_MIDDLEWARES = { 'weibo.middlewares.CookiesMiddleware': 543, } 中间件内容 class CookiesMid ...
- centos 命令和
一.远程工具 Window系统上 Linux 远程登录客户端有SecureCRT, Putty, SSH Secure Shell.TightVNC... 重点推荐一款 FinallShell,一般人 ...
- mybatis和spring整合的关键配置
spring配置文件 applicationContext.xml: <beans xmlns="http://www.springframework.org/schema/beans ...
- Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
# yum install -y vim Loaded plugins: fastestmirror, presto Loading mirror speeds from cached hostfil ...
- mysql数据库基于LVM快照的备份
lvm-snapshot: 基于LVM快照的备份 1.事务日志跟数据文件必须在同一个卷上 2.创建快照卷之前,要请求mysql的全局锁,在快照创建完成之后释放锁 3 ...
- sqlserver循环
普通while循环 1 循环5来修改学生信息 循环遍历修改记录 DECLARE @i int set @i=0 while @i<5 BEGIN update Student set demo ...