排列組合演算法

2021-09-08 16:25:53 字數 2838 閱讀 6272

排列:從n個不同元素中,任取m(m<=n)個元素按照一定的順序排成一列,叫做從n個不同元素中取出m個元素的乙個排列;從n個不同元素中取出m(m<=n)個元素的所有排列的個數,叫做從n個不同元素中取出m個元素的排列數,用符號a(n,m)表示。 a(n,m)=n(n-1)(n-2)……(n-m+1)= n!/(n-m)! 此外規定0!=1

組合:從n個不同元素中,任取m(m<=n)個元素並成一組,叫做從n個不同元素中取出m個元素的乙個組合;從n個不同元素中取出m(m<=n)個元素的所有組合的個數,叫做從n個不同元素中取出m個元素的組合數。用符號c(n,m) 表示。 c(n,m)=a(n,m)/m!=n!/((n-m)!*m!);  c(n,m)=c(n,n-m)。

#include using

namespace

std;

#define maxn 10

char

used[maxn];

intp[maxn];

char

s[maxn];

//從n個元素中選r個進行排列

void permute(int pos,const

int n,const

intr)

for (i=0; i)

}}//

從n個元素中選r個進行組合

void combine(int pos,int h,const

int n,const

intr)

for(i=h; i<=n-r+pos; i++) /*

對於所有未用的元素

*/ }}//

產生0~2^r-1的二進位制序列

void binary_sequence(int pos,const

intr)

p[pos] = 0

; binary_sequence(pos+1

,r);

p[pos] = 1

; binary_sequence(pos+1

,r);}//

利用上面的二進位制序列列印字串的所有組合

//如"abc"輸出a、b、c、ab、ac、bc、abc。

void all_combine(int pos,const

intr)

cout

; }

p[pos] = 0

; all_combine(pos+1

,r);

p[pos] = 1

; all_combine(pos+1

,r);}//

利用r進製序列列印字串的所有重複組合

//如"abc"輸出aaa、aab、aac、aba、abb、abc、aca、acb、acc...。

void repeative_combine(int pos,const

intr)

cout

; }

for(i=0; ii)

}int

main()

#include using

namespace

std;

template

void permute(type a, int start, int

end)

cout

<}

else

}}template

void combine(type a, bool b, int start, int

end)

cout

<}

else

}int

main()

;

int n = 3

; cout

<<"

permute:

"

0,n-1

); cout

<<"

combine:

"

]; combine(p,b,

0,n-1

);

return0;

}

c++ stl中提供了next_permutation和prev_permutation演算法。因為next_permutation和prev_permutation實際上是一樣的,因此只描述next_permutation演算法。next_permutation()函式的作用是取下乙個排列組合。考慮的全排列:abc,acb,bac,bca,cab,cba,以「bac」作為參考,那麼next_permutation()所得到的下乙個排列組合是bca,prev_permutation()所得到的前乙個排列組合是「acb」,之於「前乙個」和「後乙個」,是按字典進行排序的。

next_permutation()演算法描述:

從str的尾端開始逆著尋找相鄰的元素,*i和*ii,滿足*i<*ii;

接著,又從str的尾端開始逆著尋找一元素,*j,滿足*i>*j(*i從步驟一中得到);

swap(*i,*j);

將*ii之後(包括*ii)的所有元素逆轉。

舉個例子,需要找到「01324」的下乙個排列,找到*i=2,*ii=4,*j=4,下乙個排列即「01342」。再來找到「abfedc」的下乙個排列,找到*i=b,*ii=f,*j=c,swap操作過後為「acfedb」,逆轉操作過後為「acbdef」。

//

求階乘int factorial(int

n)template

void print(type a, int

n)template

void perm2(type a, int

n)}

排列組合演算法

在開發的過程中很難免會到排列組合,剛開始通過for迴圈來搞定。但是對於工作了近五年的我而已,不能像 新人那樣做了。如果所要組合的集合大於40,你不可能寫40個for迴圈吧!這裡使用了數學的演算法,到底是啥演算法,高人也沒說!不過我把它的思想提公升了一下。如下 ifndef combination h...

排列組合演算法

1.排列演算法 include define max num 100 void print int n,int num void swap int a,int b int arrange int i,int n,int num i return count int combination int u...

排列組合演算法

組合演算法的思路是開乙個陣列,其下標表示1到m個數,陣列元素的值為1表示其下標代表的數被選中,為0則沒選中。初始化,將陣列前n個元素置1,表示第乙個組合為前n個數。從左到右掃瞄陣列元素值的 10 組合,找到第乙個 10 組合後將其變為 01 組合,同時將其左邊的所有 1 全部移動到陣列的最左端。當第...