HDOJ 1716 排列2 next_permutation函数
Problem Description
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。
Sample Input
1 2 3 4
1 1 2 3
0 1 2 3
0 0 0 0
Sample Output
1234 1243 1324 1342 1423 1432
2134 2143 2314 2341 2413 2431
3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321
1123 1132 1213 1231 1312 1321
2113 2131 2311
3112 3121 3211
1023 1032 1203 1230 1302 1320
2013 2031 2103 2130 2301 2310
3012 3021 3102 3120 3201 3210
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int a[6],vis[5],t,sol[5],last;
void dfs(int c)
{
if(c==5)
{
if(sol[1]==0)
return; //千位是0的不符合
if(t!=0 && sol[1]==last)
printf(" "); //一行的最后一个数后面不能输出空格
if(t!=0 && sol[1]!=last)
{
printf("\n"); //千位不同则换行
}
last=sol[1]; //记录上一个的千位
int j=1;
for(j; j<=4; j++)
{
printf("%d",sol[j]);
}
t++;
}
int i;
for(i=1; i<=4; i++)
{
if(vis[i]==0)
{
vis[i]=1;
sol[c]=a[i];
dfs(c+1);
vis[i]=0;
while(a[i]==a[i+1]) //关键: 去重复
i++; //因为题目的输入是从小到大(否则先排序),所以如果在一轮排列结束后
} //如果后一个将要被选的数与上一轮这个下标的数相同,则跳过
} //比如1 1 1 2,第一轮1 1 1 2,返回上一次dfs后sol[3]=2,sol[4]=1
} //返回到cnt=2,此时a[2]==a[3]=1,若再选1则为1 1 1 2,跳过则为1 2 1 1
int main()
{
int i,T=0;
while(scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]))
{
if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0) break;
if(T!=0) printf("\n");
T=1;
memset(vis,0,sizeof(vis));/**vis(),记录那个数有没有用过**/
t=0;
dfs(1);
printf("\n");
}
return 0;
}
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int a[5],i,j,k,m=0;
while(~scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]))
{
if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0)
return 0;
if(m)
printf("\n");
m=1;
k=1;int h;
do
{
if(a[1]==0)
continue;
if(k==1)
{
printf("%d%d%d%d",a[1],a[2],a[3],a[4]);
k=0;
}
else
{
if(h==a[1])
printf(" %d%d%d%d",a[1],a[2],a[3],a[4]);
else
printf("\n%d%d%d%d",a[1],a[2],a[3],a[4]);
}
h=a[1];
}
while(next_permutation(a+1,a+5));/**prev_permutation按降序排列,别忘了头文件#include<algorithm>**/
printf("\n");
}
return 0;
}
next_permutation 需要头文件#include <algorithm>
这是一个求一个排序的下一个排列的函数,可以遍历全排列.
next_permutation实现原理
在《STL源码解析》中找到了这个函数,在此也简单叙述一下原理:
在STL中,除了next_permutation外,
所谓“下一个”和“上一个”,书中举了一个简单的例子:对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,c比b大,
它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},同理可以推出所有的六个序列为:
{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c,b, a},其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
int 类型的next_permutation
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
do
{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
}
while (next_permutation(a,a+3));//参数3指的是要进行排列的长度
//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
return 0;
}
输出:
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
若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
int list[3]={3,2,1};
next_permutation(list,list+3);
cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
//输出: 1 2 3
HDOJ 1716 排列2 next_permutation函数的更多相关文章
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- HDOJ 1716 排列2(next_permutation函数)
Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...
- c++中STL中的next_permutation函数基本用法
对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...
- 全排列函数 nyoj 366(next_permutation()函数)
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- POJ-1256 next_permutation函数应用
字典序列: 在字典序中蕴含着一个点,就是大小的问题,谁先出现,谁后出现的问题.譬如a<b<c,出现顺序就是a,b,c. 本题中字符集是所有大小写字母,而题目中规定的谁大谁小已经不是按asc ...
- hdu 1716 排列2(DFS搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Time Limit: 1000/1000 MS (Java/Others) Me ...
- next_permutation()函数 和 prev_permutation() 按字典序求全排列
next_permutation功能: 求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...
- 【HDOJ】1716 排列2
STL. /* 1716 */ #include <iostream> #include <algorithm> #include <cstdio> #includ ...
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
随机推荐
- [JS] JavascriptHelp (转载)
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- 菜鸟日记之 java中的集合框架
java中的集合框架图 如图所示:java中的集合分为两种Collection和Map两种接口 可分为Collection是单列集合和Map的双列集合 Collection单列集合:继承了Iterat ...
- Mysql 操作手册
mysql操作手册 版本:5.6.16mysql linux安装基本步骤:#rpm -e --nodeps mysql-lib-5.1.*#rpm -ivh mysql-server#rpm -ivh ...
- Codevs 4768 跳石头 NOIP2015 DAY2 T1
4768 跳石头 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 一年一度的"跳石头"比赛又要开始了! ...
- 自然数e为底数的指数函数的一个小运用
以自然数e为底数的指数函数: 半衰期: 倍增期:
- HDU3591找零,背包
题目大概的意思就是:小强用硬币买东西,硬币有N种,面值为Vi,店家有各种硬币都有无限个,而小强只有Ci个(分别对应Vi) 问最小交易硬币数,就是一个有找零的背包问题啦. 我的上一篇博客跟这hdu359 ...
- gnome中文翻译之po
文件类型: po: 用msginit分析pot文件,生成各语言对应的po文件,比如中文的zh_CN.po. mo: 用msgfmt将po文件编译生成mo文件,这是二进制文件,不能直接编辑. gmo: ...
- Linux环境下添加ftp账号步骤
(1)远程登录Linux服务器所用的工具,免费开源,可以从网站上很容易就下载到. (2)打开putty,输入服务器IP,进入后按提示进入用户名和密码输入超级管理员 root,然后系统让输入密码,注意此 ...
- GoogleCode新手教程
GoogleCode页面介绍 Project Home 首先显示的是project home,页面左边的是这个项目的介绍,右边的License是说明使用的是什么开源协议,Labels是标签的意思,就是 ...
- bootstrap 下 标签页跳转总结
最近遇到一个问题,是关于bootstrap中的标签页实现上的一些功能实现,现总结一下. 问题描述:点击其他标签页后,如何在点击搜索按钮后自动跳转到第一个标签页.如下图 通过对bootstrap框架里的 ...