# Include <iostream> # include <string> # include <math. h ># include <vector >#include <algorithm> using namespace STD; // method1bool flag [5]; int arr [5] = {1, 2, 3, 4, 5 }; int Len = sizeof (ARR)/sizeof (INT); void comb (int n, int count); // method2void comb2 (int n, int count ); vector <int> result; // method3: void comb3 (int n, int count); int getcombcount (int n, int m); // 3 void getcharcomb (); // 4int data [3] = {1, 2, 3}; int data1 [3] = {4, 2, 3}; void permutation (int * a, int Len); void stl_permutaton (int * a, int Len ); // you can obtain comb () + empty set // 5int num = 0; void permutation (INT array [], int begin, int end); int main () {vector <char> result; For (INT I = 0; I <5; I ++) Flag [I] = false; cout <"--- recursive combination 1 (print each Combination Based on the flag) ----" <Endl; comb ); cout <"--- recursive combination 2 (save each combination to the container) ---" <Endl; comb2 (); cout <"--- non-recursive combination ---" <End L; comb3 (5, 3); cout <"--- all combinations of strings (idempotent except empty strings) ----" <Endl; getcharcomb (); cout <"--- non-recursive arrangement ---" <Endl; permutation (data, 3); cout <"---- STL full arrangement --" <Endl; stl_permutaton (data1, 3); cout <"-- Recursive arrangement ----" <Endl; int A [3] = {2, 3, 4}; permutation (A, 0, sizeof () /sizeof (INT)-1); Return 0;} void comb (int n, int count) {If (COUNT = 0) {// simiar with select using vector storage selected data will be similar Epoll for (INT I = 0; I <Len; I ++) if (flag [I] = true) cout <arr [I] <""; cout <Endl; return; //-exit condition 1 -- (recursive end condition 1)} If (n <0) // exit condition 2 -- (recursive end condition 2) return; flag [N] = true; comb (n-1, Count-1); flag [N] = false; comb (n-1, count);} void comb2 (int n, int count) {If (COUNT = 0) {// a combination obtained after each recursive end, vector <int>: iterator it; for (IT = result. begin (); It <result. end (); It ++) // cout <* It <""; cout <End L; return;} If (n <0) return; result. push_back (ARR [N]); comb2 (n-1, Count-1); result. pop_back (); comb2 (n-1, count);}/* use an array to simulate a binary calculator. If one is 1, the corresponding character is obtained. If the value is 0, no value is obtained, character combinations can be implemented. You can also remove arrays. It can contain N characters. Int num is increased from 1 to 2 ^ n-1, and num is shifted to I-bit right, followed by 1 to perform the bitwise & operation to determine whether to take the I character or not. 001 010 011 100 101 110 111c B bc a ac AB ABC // the problem still exists is the length of the character array <32, this method is still very good, if it is greater than 32, the original method is required. */Void getcharcomb () {// evaluate the idempotent set // string STR = "AABC"; string STR = "ABC"; int n = Str. size (); int num = POW (2.0, n); // (2.0 N) for (INT I = 1; I <num; I ++) // num = [1-7] {for (Int J = 0; j <n; j ++) {If (I> J) & 1) // tips ----: Bit cout detected as 1 <STR [J]; // ---- STR [J] is first a} cout <Endl ;}} /* lexicographic changes (1234 ----> 4321) 1. compare the adjacent elements from the rightmost side until find the two adjacent elements on the right that are larger than the left. is to be replaced, and then find the first one greater than this element from the rightmost side, and exchange the two three. after switching, flip all elements after the switching element */void permutation (int * a, int Len) {Int I, j; int TMP; int num = 6; // 1*2*3 copy (ARR, arr + Len, ostream_iterator <int, char> (cout, ""); cout <Endl; for (I = len-1; I> 0; I --) {if (a [I]> A [I-1]) // I-1 [1] {for (j = len-1; j> = 0; j --) if (a [J]> A [I-1]) // [2] {TMP = A [I-1]; A [I-1] = A [J]; A [J] = TMP; // [3] int M = I, N = len-1; while (M <= N) {TMP = A [m]; A [m] = A [n]; A [n] = TMP; m ++; N --;} break;} I = Len; // begin againcopy (A, A + Len, ostream_iterator <int, char> (cout, ""); cou T <Endl ;}}void stl_permutaton (int * a, int Len) {// the first sequence of the Lexicographic Order must be incremented by sort (A, A + Len ); do {copy (A, A + Len, ostream_iterator <int, char> (cout, ""); cout <Endl ;}while (next_permutation (, A + Len);}/* Train of Thought: (A, B, C, D) The full row is column 1, followed by a (B, c, d) B and C (A, C, D). 3. C and C (A, B, and d) the full arrangement of 4, D followed by (A, B, C) and the (B, c, d) of 1 can still be divided according to the above form. */Void permutation (INT array [], int begin, int end) {int I; If (begin = end) {// recursive exit condition processing the last element has num ++; // cout <End <Endl; for (I = 0; I <= end; ++ I) {cout <array [I] <";}cout <Endl; return ;} else {// For Loop traverses all possible conditions at the first position in the arrangement for (I = begin; I <= end; ++ I) {swap (array [I], array [begin]); permutation (array, begin + 1, end); swap (array [I], array [begin]); // restore the array to continue the next traversal }}// non-recursive method to get combinationvoid comb3 (int n, int count) {/* Minimum variation of decreasing: find the first 10-to-10 switch from the right to the left, indicating that I want to reduce the number but I want to reduce the number at least, then the number behind the switch point should be as large as possible, so move all 1 to the high position after the switching point 11100110101100110110101011001101110011010101100111 * // int bit [5] = {, 0 }; // initial bit arrayint * bit = new int [N]; for (int K = 0; k <n; k ++) {If (k <count) bit [k] = 1; elsebit [k] = 0;} int I, j, beg, end; int Len = sizeof (ARR)/sizeof (INT ); int n = getcombcount (n, count); // C (n, count) C (5, 3) for (I = 0; I <Len; I ++) if (bit [I] = 1) cout <arr [I]; cout <Endl; For (j = 1; j <= N-1; j ++) {for (I = len-1; I> 0; I --) {If (bit [I] = 0 & bit [I-1] = 1) {swap (bit [I], bit [I-1]); // from index: [I to len-1], make all bit 1 in the rightbeg = I; end = len-1; while (1) {While (bit [beg] = 1) {beg ++; If (beg> = Len) break ;} while (bit [end] = 0) {end --; If (end <I) break;} If (beg <End) Swap (bit [beg], bit [end]); elsebreak;} // end of "while" break;} // end of "if" // copy (bit, bit + 5, ostream_iterator <int, char> (cout, ""); // cout <Endl ;}for (I = 0; I <Len; I ++) if (bit [I] = 1) cout <arr [I]; cout <Endl ;}} int getcombcount (int n, int m) {int I; int A, B, C, S; // s = A/(B * C) a = B = c = 1; for (I = 1; I <= N; I ++) A * = I; for (I = 1; I <= m; I ++) B * = I; for (I = 1; I <= N-m; I ++) C * = I; S = A/(B * C); Return s ;}
Summary of permutation and combination recursive and non-Recursive Algorithms