next_permutation()
2016-11-11 19:51
197 查看
next_permutation()是一个过程,
也是一个函数,
需要#include <algorithm>
next_permutation(a,a+k); 从a[0]开始,对总共k个数进行升序排序;
next_permutation(a+i,a+n); 从a[i]开始,一直排到a
;
感觉很奇怪, 这里有点问题
因为按照这么理解
第一个函数应该是从a[0]排到a[k],总共k+1个数,这就尴尬了
但是实验下来,确实这么用,无语
也是一个函数,
需要#include <algorithm>
next_permutation(a,a+k); 从a[0]开始,对总共k个数进行升序排序;
next_permutation(a+i,a+n); 从a[i]开始,一直排到a
;
感觉很奇怪, 这里有点问题
因为按照这么理解
第一个函数应该是从a[0]排到a[k],总共k+1个数,这就尴尬了
但是实验下来,确实这么用,无语
1 int main() 2 { 3 int a[3]; 4 a[0]=1;a[1]=2;a[2]=3; 5 do 6 { 7 cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl; 8 } while (next_permutation(a,a+3)); 参数3指的是要进行排列的长度 9 10 如果a不是最大的排列,就返回true。如果a是最大的排列,返回false, 11 每执行一次,a就变成下一个更大的排列 12 13 } 14 15 输出: 16 17 1 2 3 18 1 3 2 19 2 1 3 20 2 3 1 21 3 1 2 22 3 2 1 23 24 25 如果改成 while(next_permutation(a,a+2)); 26 则输出: 27 1 2 3 28 2 1 3 29 30 只对前两个元素进行字典排序 31 32 显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3 33 34 35 若排列本来就是最大的了,则next_permutation执行后,会对排列进行字典升序排序 36 37 int list[3]={3,2,1}; 38 next_permutation(list,list+3); 39 cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl; 40 41 输出: 1 2 3
相关文章推荐
- LeetCode--next-permutation
- next_permutation(全排列算法)
- POJ-2718-Smallest Difference(next_permutation)
- Ignatius and the Princess II (HDU 1027) ——next_permutation排列函数
- 字典序与next_permutation
- LeetCode OJ--Next Permutation *
- HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】
- next_permutation 实现
- TOJ 2960.VECI-- next_permutation
- 全排列函数(next_permutation)
- STL 排序算法之全排列sort和next_permutation
- next_permutation 和 一个不成功的案例
- next_permutation原理剖析
- next_permutation生成全排列打表法解决问题
- 【STL】next_permutation的实现
- next_permutation应用
- next_permutation原理剖析
- 全排列生成算法:next_permutation
- LeetCode Next Permutation
- 不知道会后悔的函数 next_permutation