#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int a[];
void print_permutation(int n, int a[], int cur){
int i, j;
if(cur == n){
for(i = ; i < n; ++i) printf("%d ",a[i]);
printf("\n");
}
else for(i = ; i <= n; ++i){
bool flag = true;
for(j = ; j < cur; ++j) if(a[j] == i) flag = false;
if(flag){
a[cur] = i;
print_permutation(n, a, cur+);
}
}
}
int main(){
print_permutation(, a, );
return ;
}

使用STL的next_permutation()会更加方便:

next_permutation实现原理

在《STL源码解析》中找到了这个函数,在此也简单叙述一下原理:

在STL中,除了next_permutation外,还有一个函数prev_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}没有下一个元素。

next_permutation的函数原型如下:

template<class BidirectionalIterator>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template<class BidirectionalIterator, class BinaryPredicate>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);

对于第二个重载函数的第三个参数,默认比较顺序为小于。如果找到下一个序列,则返回真,否则返回假。

所以,这道题目的代码可以这么写:

 #include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,A[],cur=;
cin >> n;
int i;
for(i=; i<n; ++i) A[i]=i+;
do{
for(i=; i<n; i++) cout<<A[i]<<' ';
cout<<endl;
}
while(next_permutation(A,A+n));
return ;
}

小结

用next_permutation和prev_permutation求排列组合很方便,但是要记得包含头文件#include <algorithm>。

虽然最后一个排列没有下一个排列,用next_permutation会返回false,但是使用了这个方法后,序列会变成字典序列的第一个,如cba变成abc。prev_permutation同理。

7.2.1 生成1~n的排列(全排列)【STL__next_permutation()_的应用】的更多相关文章

  1. 生成1~n的排列,以及生成可重集的排列

    #include <iostream> using namespace std; void printPermutation(int n, int* A, int cur) { if (c ...

  2. 生成1~n的排列(模板),生成可重集的排列(对应紫书P184, P185)

    生成1~n的排列: #include<iostream> using namespace std; void print_permutation(int n, int *A, int cu ...

  3. LeetCode 笔记21 生成第k个排列

    题目是这样的: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all ...

  4. MVC扩展生成CheckBoxList并水平排列

    本篇体验生成CheckBoxList的几个思路,扩展MVC的HtmlHelper生成CheckBoxList,并使之水平排开.     通过遍历从控制器方法拿到的Model集合 □ 思路 比如为一个用 ...

  5. 生成1~n的排列

    直接递归打印. 代码如下 #include<cstdio> void dfs(int *a,int cur,int n) { if(cur==n) { for(int i=0;i<n ...

  6. 生成n个元素的全排列 C实现

    近期在准备复习算法设计的考试,下边记录一些,看笔记时突然想到的解法. 问题是这种 用递归实现 n 个元素的全排列. 当时老师给出的解答是 假定第i个元素 ri 放在首位,于是 f(r1,r2,-,rn ...

  7. 剑指offer-面试题38-字符串的排列-全排列

    /* 题目: 输入字符串,打印字符串的所有排列. 输入acc,输出[acc, cac, cca]. */ /* 思路: 将字符串看作两部分,第一个字符串和后面的部分. 将第一个字符串与后面字符串依次交 ...

  8. dfs 生成排列和组合

    利用深度优先搜索的性质可以方便的生成n的排列和组合,但是生成组合时每个组合里面元素的个数必须事先确定,以前以为生成组合跟排列一样到n时就可以回溯,直到今天做了某题之后才发现那是错的,那样做生成不了所有 ...

  9. HDU 1027 Ignatius and the Princess II 排列生成

    解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...

随机推荐

  1. hdu 4638 Group 莫队算法

    题目链接 很裸的莫队, 就不多说了... #include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) # ...

  2. Fedora 17配置ssh及Windows远程连接

    转载自:http://nanjingjiangbiao-t.iteye.com/blog/1794213 Fedora 23 默认已经安装好openssh server了,不用再装不过默认情况下没有开 ...

  3. Aone新拉分支

    1.进入Aone新建项目 2.测试人员填huyangjun和husong 3.进入后拉分支 4.弄个日常普通环境 5.吧环境跑起,绑定Host就可以

  4. C#手机充值

    C#手机充值系统开发(基于聚合数据) 说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: ...

  5. HDU 2722 Here We Go(relians) Again

    最短路,建图太麻烦,略过…… #include <cstdio> #include <cstring> #include <queue> const int INF ...

  6. 对于java用发送http请求,请求内容为xml格式

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...

  7. C#面向对象编程基础-喜课堂笔记

    **************[5][C#面向对象编程基础]第1讲:类与对象****************                 *************2.1.1_类与对象的概念**** ...

  8. 基于maven插件的缓存控制插件

    asset-cache-control github源码及下载地址: https://github.com/StruggleBird/asset-cache-control 基于maven插件的缓存控 ...

  9. CAEmitterLayer 粒子发射器

    在iOS 5中,苹果引入了一个新的CALayer子类叫做CAEmitterLayer.CAEmitterLayer是一个高性能的粒子引擎,被用来创建实时例子动画如:烟雾,火,雨等等这些效果. CAEm ...

  10. C-C Radar Installation 解题报告

    C-C    Radar Installation   解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#pr ...