C++全排列函数next_permutation()和prev_permutation()
头文件:#include<algorithm>
* * *
1. next_permutation():
next_permutation()函数的返回类型是bool类型.
即:如果有一个更高的排列,它重新排列元素,并返回true;如果这是不可能的(因为它已经在最大可能的排列),它按升序排列重新元素,并返回false。
使用:
next_permutation,重新排列范围内的元素[第一,最后一个)返回按照字典序排列的下一个值较大的组合。
next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。
算法描述:
从尾部开始往前寻找两个相邻的元素
第1个元素i,第2个元素j(从前往后数的),且i<j
2、再从尾往前找第一个大于i的元素k。将i、k对调
3、[j,last)范围的元素置逆(颠倒排列)
2. prev_permutation():
prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。
例:
K—排列2
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<iostream>
#include<algorithm> //next_permutation
using namespace std;
int deal(int n)
{
int sum=;
for (int i=;i<=n;i++)
sum*=i;
return sum;
}
int main()
{
int x[],f,flag=;
while ()
{ cin>>x[]>>x[]>>x[]>>x[];
if(x[]==&&x[]==&&x[]==&&x[]==)
return ; if(flag!=)
cout << endl;
flag=;
int w=;
sort(x,x+); //排序,默认升序,头文件algorithm
f=;
do
{
if (x[]==)
continue;
if (f==)
{
cout << x[] << x[] << x[] << x[] ;
f=;
}
else
{
if (w==x[])
cout << ' ' << x[] << x[] << x[] << x[] ;
else
cout << endl << x[] << x[] << x[] << x[] ; }
w=x[];
}
while (next_permutation(x,x+));
cout << endl;
}
return ;
}
注意输出格式:是输入一组数据后换行输出结果
C++全排列函数next_permutation()和prev_permutation()的更多相关文章
- C++中全排列函数next_permutation用法
最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...
- C++中全排列函数next_permutation 用法
今天蓝桥杯刷题时发现一道字符串排序问题,突然想起next_permutation()函数和prev_permutation()函数. 就想写下next_permutation()的用法 next_pe ...
- HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 全排列函数next_permutation(a,a+n)
#include<iostream> #include<algorithm> using namespace std; int main(){ ]; int n; cin> ...
- STL中关于全排列next_permutation以及prev_permutation的用法
这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...
- next_permutation() 全排列函数
next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...
- C++ STL 全排列函数详解
一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...
- C++ STL 全排列函数
C++ 全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
随机推荐
- rabbitmq参考文档
英文文档:http://www.rabbitmq.com/getstarted.html 中文文档:http://rabbitmq.mr-ping.com/ rabbitmq重启,消费者恢复,解决消费 ...
- WOW.js 和 animate.css 使用
animate.css 动画样式,用户也可以非常容易修改设置喜欢的动画库. Wow.js 允许用户滚动页面的时候展示 CSS 动画.配合animate.css ,做出很棒的效果,它支持 animate ...
- 吴裕雄--天生自然 JAVA开发学习:抽象类
public abstract class Employee { private String name; private String address; private int number; pu ...
- 基础篇九:模块介绍(--with-http_stub_status_module)
下面--with 即为编译安装的模块 下面我们来介绍--with-http_stub_status_module此模块 vim /etc/nginx/conf.d/default.conf 然后检查 ...
- [LC] 285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- git基本操作-长期维护
############### 初识git ############## """ git初识: 1,git是什么?git是一个帮助用户版本控制的软件, 2,g ...
- 有几张高度不一样的小图片,如何用html+css实现在同一行垂直居中对齐?
方法一 :使用弹性布局 方法二 :CSS3 transform 属性 父级元素定位 子集元素加属性: position: absolute; top: 50%; transform: translat ...
- Elasticsearch Rest模式和RPC模式性能比较
Elasticsearch 有两种链接模式,即Rest方式(对应端口9200)和RPC方式(对应端口9300)这两种访问效率到底差多少,在同样的业务逻辑下,测试了一波. 用的JMeter进行压力测试 ...
- SQLserver安装了多个实例,实例端口号也被修改后的代码连接方式
事例说明 IP地址为192.168.1.100的服务器上安装了SQLServer2000,它有两个实例,分别为:默认实例——JLW和另外新建的实例——JLW\TEST SQLServer网络实用工具中 ...
- RHCSA考试(Linux7)
博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽 一.设置环境: 请初始化您的考试虚拟机 server0.example.com,将系统的 root 账号密码设置为 12 ...