next_permutation(排列组合)函数的用法
2017-10-25 21:37
429 查看
原文:http://blog.csdn.net/fk103/article/details/44993125
next_permutation函数
组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start,end)。这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后,严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.
对于next_permutation函数,其函数原型为:
#include <algorithm>
bool next_permutation(iterator start,iterator end)
当当前序列不存在下一个排列时,函数返回false,否则返回true
我们来看下面这个例子:
[cpp] view
plain copy
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[3]={1,2,3};
do
{
cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
}while(next_permutation(num,num+3));
return 0;
}
输出结果为:
![](https://img-blog.csdn.net/20150427130510043?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQUNfR2lic29u/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
当我们把while(next_permutation(num,num+3))中的3改为2时,输出就变为了:
![](https://img-blog.csdn.net/20150427130713826?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQUNfR2lic29u/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
由此可以看出,next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值。
另外,需要强调的是,next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。比如,如果数组num初始化为2,3,1,那么输出就变为了:
![](https://img-blog.csdn.net/20150427131704330?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQUNfR2lic29u/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
此外,next_permutation(node,node+n,cmp)可以对结构体num按照自定义的排序方式cmp进行排序。
关于该函数的用法,不妨先看下这道题
![](http://59.69.128.200/JudgeOnline/admin/kind/plugins/emoticons/18.gif)
,,,聪明的你能帮小明解围吗?
特定顺序:每一个组合中的值从小到大排列,组合之间按字典序排列。
2
3
21
123
132
213
231
312
321
代码如下
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
3 1
4 2
3 3
2
3
12
13
14
21
23
24
31
32
34
41
42
43
123
132
213
231
312
321
代码如下
其中的substr是取字符串某一部分的函数;
next_permutation函数
组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start,end)。这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后,严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.
对于next_permutation函数,其函数原型为:
#include <algorithm>
bool next_permutation(iterator start,iterator end)
当当前序列不存在下一个排列时,函数返回false,否则返回true
我们来看下面这个例子:
[cpp] view
plain copy
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[3]={1,2,3};
do
{
cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
}while(next_permutation(num,num+3));
return 0;
}
输出结果为:
当我们把while(next_permutation(num,num+3))中的3改为2时,输出就变为了:
由此可以看出,next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值。
另外,需要强调的是,next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。比如,如果数组num初始化为2,3,1,那么输出就变为了:
此外,next_permutation(node,node+n,cmp)可以对结构体num按照自定义的排序方式cmp进行排序。
关于该函数的用法,不妨先看下这道题
题目描述
一天ZJ看到小明竟然排序学的比自己还牛,于是就出了一道题来难为小明,来证明自己排序学的比小明更牛。题目就是有一个数n(0<n<10),写出1到n的全排列,这时小明有点囧了![](http://59.69.128.200/JudgeOnline/admin/kind/plugins/emoticons/18.gif)
,,,聪明的你能帮小明解围吗?
输入
第一行输入一个数N(0<N<10),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个整数x(0<x<10)输出
按特定顺序输出所有组合。特定顺序:每一个组合中的值从小到大排列,组合之间按字典序排列。
样例输入
22
3
样例输出
1221
123
132
213
231
312
321
代码如下
输入
第一行输入整数N(1<N<10)表示多少组测试数据,每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
输出
在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例样例输入
33 1
4 2
3 3
样例输出
12
3
12
13
14
21
23
24
31
32
34
41
42
43
123
132
213
231
312
321
代码如下
其中的substr是取字符串某一部分的函数;
相关文章推荐
- next_permutation(排列组合)函数的用法
- [C++] next_permutation的函数 与 copy() 函数的用法
- [C++] next_permutation的函数 与 copy() 函数的用法
- 关于全排列 next_permutation() 函数的用法
- STL之全排列next_permutation()函数的用法
- stl全排列next_permutation()与prev_permutation()函数用法
- 有关next_permutation()函数的用法, poj 2718
- 详谈全排列next_permutation() 函数的用法(推荐)
- 关于全排列 next_permutation() 函数的用法
- C++中全排列函数next_permutation 用法
- 关于全排列 next_permutation() 函数的用法
- C++ STL 之 next_permutation 的用法(下一个序列函数,按字典序排)
- 全排列函数next_permutation的用法
- next_permutation函数介绍
- next_permutation 与 prev_permutation的用法 (全排列)
- 对char * const *(*next)(); char * (*c[10])(int **p)的理解从而引发的对函数指针的理解以及const修饰符的用法理解
- hdu1716(库函数next_permutation)
- 全排列函数next_permutation
- next_permutation()的用法,与作用
- next_permutation的函数