Combination algorithm combination source code

zhaozj2021-02-16  71

// Combination Algorithm Implementation

// copyright (c) 2004, benbear //// this file is an algorithm of the combination. This library is free // software; you can redistribute it and / or modify it under the terminal @ i g 正 public license AS Published by the Free Software // Foundation; Either Version 2, or (at Your Option) Any Later // Version.

// This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY;. Without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE See the GNU // General Public License for more details.

// YOULD HAVE RECEIVED A COPY The GNU General Public License // Along with this library; see the file copying. If not, write to // the free software fit - Suite 330, Boston, // Ma 02111-1307, USA.

#include

Namespace benbear {

Using namespace std;

Template Voidsort_combination (Biiterator First, Biiterator Last) {if (first == last) // no element return;

Biiterator i = first; i; if (i == last) // one element return; int hALF = distance (first) / 2; // Half of the length biiterator middle = first; advance (Middle, Half ); // middle = HALF

Sort_combination (first, middle); // sort first part sort_combination (middle, last); // Sort Second Part

INPLACE_MERGE (First, Middle, Last); // Merge Two Parts}

template voidadjust_combination (BiIterator first, BiIterator middle, BiIterator last) {// the front part or the back part have no elements if ((first == middle) || (middle == last)) return;

Sort_combination (first, middle); sort_combination (middle, last); biiterator b = middle; --b; biiterator j = lower_bound (middle, last, * b); Reverse (J, Last); Reverse (Middle, Last); }

Template Voidinit_Combination (Biiterator First, Biiterator Middle, Biiterator Last, Bool MIN) {Sort_combination (First_Combination (First, Last);

IF (min == false) {// The max combination reverse (first, last); Reverse (first, middle);}}

Template Boolnext_combination (Biiterator First, Biiterator Middle, Biiterator Last) {IF ((First == Middle) | (Middle == Last) Return False;

// Last Element of [first, middle) Biiterator B = middle; --b;

IF (* B <* middle) {Biiterator J = B; While ( B! = Last) && (* j <* b)) {i_swap (j, b); j = b;} return true;}

Biiterator E = last; --e; while (e! = Middle) {Biiterator K = E; --K; if (! (* K <* e)) E = K; Else Break;} Biiterator F = E; f; while ((f! = last) &&! (* f <* e)) f;

IF (! (* first <* e)) {Reverse (first, middle); Reverse (first, last); Return False;}

IF (* b <* e) {Biiterator BB = B; While ( BB! = E) &&! (* B <* bb)); Reverse (bb, f); Reverse (b, f);} Else {Biiterator I = B; While (! (* - i <* e)); Biiterator J = Last; While (! (* i <* - j));

Iter_Swap (i, j); Reverse ( i, middle); Reverse (i, j);} return true;}

Template BoolPrev_combination (Biiterator First, Biiterator Middle, Biiterator Last) {IF ((First == Middle) || (Middle == Last)) Return False; Biiterator B = Middle; --b; if (* Middle) <* b) {BIITERATOR I = Upper_bound (first, middle, * middle); if (i! = b) iter_swap (i, middle); else {biiterator s = middle; while (( s! = last) && ! (* s <* middle)); Reverse (b, s);} return true;} Biiterator E = Last; --e; while (e! = middle) {Biiterator K = E; --K; IF ! (* k <* e)) E = K; Else Break;} Biiterator F = E; F; While ((f! = last) &&! (* f <* e)) f;

IF (f == last) {Reverse (first, last); Reverse (first, middle); Return False;}

Biiterator i = Upper_bound (first, middle, * f); if (i == b) {Biiterator s = f; while ( s! = Last) &&! (* S <* f));

Reverse (b, f); Reverse (b, s);} else {ified_swap (i, f); Reverse ( i, f); Reverse (i, middle);} return true;}

} // end of namespace benbear

// for test: #include #include #include using namespace std;

Template VoidShow_Array (Biiterator F, Biiterator M, Biiterator L) {While (f! = m) cout << * f ; cout << ""; while (f! = L) cout << * f ; cout << Endl;}

Template VoidCombi (Biiterator F, Biiterator M, Biiterator L, Bool Next) {Benbear :: Init_combination (f, M, L, Next); INT C = 0; cout << Endl; do {show_array (f, M, L); C ;} while (next? benbear :: next_combination (f, m, l): benbear :: prev_combination (f, m, l)); cout << "The end:"; show_array (f, M, L); COUT << "there'S:" << c << endl;} intimain () {Int a [9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Bool dir = false;

For (int i = 1; i <10; i ) {random_shuffle (A, A 7); sort (a, a 3); benbear :: adjust_combination (A, A 3, A 7); show_array A, A 3, A 7);} system ("pause");

For (int i = 1, j = 9; i

Sort (a, a 9); benbear :: sort_combination (a, a 7); A [1] = 1; A [4] = 6; // a [6] = 6; for (int i = 1 , J = 7; I

INT N; COUT << "THE N:"; cin >> n; Vector vec (n); cout << "please. Input number: << endl; for (int i = 0; i > VEC [I];

For (int i = 1; i

System ("pause"); return 0;}

转载请注明原文地址:https://www.9cbs.com/read-14845.html

New Post(0)