1, combining related formulas
C (n,m) =n* (n-1) * (n-2) ... * (n-m-1)/m!=n!/(m!* (n-m)!)
C (n,m) =c (n,n-m)
C (n+1,m) =c (n,m) +c (n,m-1)
C (n,1) +c (n,2) +...+c (n,n) =2^n;
2, Correlation algorithm
Question 1: Enter a string to print out all combinations of characters in the string.
Algorithm 1: Also according to the idea mentioned in the article http://www.cnblogs.com/orchid/p/4025172.html, there is a length of strlen (s) box, each box has 0 and 1, there are a total of several different arrangement methods?
voidCombination::getcombination (int* Holders,intend) { for(intI=0;i<2; i++) {Holders[end]=i; if(End==strlen (elements)-1)//elements is a const char* type,{print (holders); } Else{getcombination (holders,end+1); } }}
Algorithm 2, without recursion, the direct loop.
voidcombination::P rintAllCombination2 () {vector<string>Holder; Holder.push_back (""); for(intI=0; I<strlen (elements); i++) { intLastone=holder.size (); for(intj=0; j<lastone;j++) { stringTemp=holder.at (j) +Elements[i]; Holder.push_back (temp); cout<< Temp <<","; } }}
Assuming that the elements inside the vector after a loop are, "" "" A "" B "" AB ", then the next loop needs to add the third character C to the sequence, i.e. insert" C "," AC "," BC "," ABC "---character C and the existing substring of the vector in the vector.
[algorithm] combinatorial class problems