// 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
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
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
IF (min == false) {// The max combination reverse (first, last); Reverse (first, middle);}}
Template
// 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
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
Template
Template
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 For (int i = 1; i System ("pause"); return 0;}