#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. React使用笔记(3)-React Event Listener

    Date: 2015-11-28 12:18 Category: Web Tags: JavaScript Author: 刘理想 [toc] 1. 构造基本结构 首先,我们先创建一个按钮,一个输入框 ...

  2. django 实现指定文件合并成压缩文件下载

    需求是在一列学生列表中选择指定的学生,然后下载他们上传的报告文档.

  3. LintCode-比较字符串

    题目描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 样例 给出 A = " ...

  4. 转;说说AngularJS中的$parse和$eval

    说说AngularJS中的$parse和$eval AngularJS的初学者常常会对$parse和$eval两个内建服务感到有些困惑,今天我们就来说说AngularJS中的$parse和$eval. ...

  5. C++的二进制兼容问题(以QT为例)

    二进制不兼容带来的问题(需要重新编译库文件,以前编译的失效): http://my.oschina.net/lieefu/blog/505363?fromerr=f5jn7rct 二进制不兼容的原理: ...

  6. 没有开发者账号,如何解锁wp8设备

    原文 http://www.cnblogs.com/vsdot/p/3263454.html 问题的引入 好了,问题的由来是这样的,我想把我开发的wp8手机部署到手机上,可是竟然出现了下面的问题: [ ...

  7. 取代奶瓶Minidwep-gtk 破 WPA 全攻略

     目录 1. CDlinux 下使用 minidwepgtk 获取握手包并使用自带的字典破解 2. 自带的字典破解不出密码时使用 U 盘外挂字典继续暴力破解密码 3. 将握手包拷贝到 Windows ...

  8. 1043 - Triangle Partitioning(数学)

    1043 - Triangle Partitioning   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit ...

  9. URAL 1260 Nudnik Photographer DFS DP

    题目:click here :这个题可以先dfs深搜下,规律dp dfs: #include <bits/stdc++.h> using namespace std; #define S ...

  10. Arduino M0 的一个坑(2015-12-25)

    前几天收到 Arduino M0,试各项功能都正常,可就是串口监视器/串口助手不能显示程序里打印的输出,好生奇怪,各种换波特率各种PC串口程序换着试,资料不多,官方资料也只说到 Serial1 用于 ...