1. 程式人生 > >next_permutation( ) 和prev_permutation( ) 全排列函數

next_permutation( ) 和prev_permutation( ) 全排列函數

algo clu start prev 都是 它的 ati end 排列組合

頭文件#include <algorithm>

兩者都是用來計算排列組合的函數。前者是求出下一個排列組合,而後者是求出上一個排列組合。

所謂“下一個”和“上一個”,有一個例子;

對序列 {a, b, c}, a > b >c,它的下一個序列即為{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( (start,end );

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

雖然最後一個排列沒有下一個排列,用next_permutation會返回false,但是使用了這個方法後,序列會變成字典序列的第一個,

如cba變成abc。prev_permutation同理

next_permutation( ) 和prev_permutation( ) 全排列函數