全排列-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”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...
随机推荐
- js设置按钮不可用
<input type="button" value="确定" id="stamp" onclick="stampBill( ...
- Jquery对当前日期的操作(格式化当前日期)
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- plist反序列化
// // ViewController.m // 03-plist文件的一个反序列化 // // Created by jerry on 15/9/28. // Copyright (c) ...
- Android五种数据存储方式
android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...
- linux笔记_day04
1.cat 连接并显示 -n 显示行号 -E END 显示行尾 2.tac 从后往前显示 3.ctrl +C 4.more 向后翻 到最后会退出 5.less 翻到最后不退出 常用 支持b k sp ...
- BN讲解(转载)
本文转载自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所讲的内容为Batch Normalization,简称BN,来源于< ...
- 提高CPU使用率100%
直接上脚本: #!/bin/bash while true do echo 2^2^20 | bc & >/dev/null done 查看CPU使用率用top命令即可 释放CPU: p ...
- vim学习、各类插件配置与安装【转】
转自:https://www.cnblogs.com/forest-wow/p/6916531.html 1. vim学习 vim基础学习:根据网上流行基础文章<简明Vim练级攻略>,进阶 ...
- 通过全备+binlog_server同步恢复被drop的库或表
MySQL 中drop 等高危误操作后恢复方法 实验目的: 本次实验以恢复drop操作为例,使用不同方法进行误操作的数据恢复. 方法: 利用master同步 :伪master+Binlog+同步(本文 ...
- ActiveMQ 入门Nodejs版
ActiveMQ 入门下载与安装 官方下载地址 解压,运行bin/win[32|64]/activemq[.bat] 启动服务 环境信息 控制台: http://localhost:8161 默认端口 ...