Advertisement
nakedman

Untitled

Oct 29th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void perm(vector<int> & values, int n = 0) {
  8.     const int count = values.size();
  9.     if (n >= count - 1) {
  10.         for (int i = 0; i < count; i++) {
  11.             cout << values[i] << " ";
  12.         }
  13.         cout << endl;
  14.     }
  15.     else {
  16.         perm(values, n + 1);
  17.         for (int i = n + 1; i < count; i++) {
  18.             swap(values[i], values[n]);
  19.             perm(values, n + 1);
  20.             swap(values[i], values[n]);
  21.         }
  22.     }
  23. }
  24.  
  25. void select(const vector<int> & values, int m, int n, vector<int> & acc) {
  26.     const int count = acc.size();
  27.     if (n >= count) {
  28.         perm(acc);
  29.     }
  30.     else {
  31.         const int values_count = values.size();
  32.         for (int i = m; i <= values_count - count + n; i++) {
  33.             acc[n] = values[i];
  34.  
  35.             select(values, i + 1, n + 1, acc);
  36.         }
  37.     }
  38. }
  39.  
  40. void process(const vector<int> & values, int count) {
  41.     vector<int> acc(count);
  42.     select(values, 0, 0, acc);
  43. }
  44.  
  45. int main() {
  46.     int n , count;
  47.     cin >> n >> count;
  48.  
  49.     vector<int> values;
  50.     values.reserve(n);
  51.     for (int i = 1; i <= n; i++) {
  52.         values.push_back(i);
  53.     }
  54.  
  55.     process(values, count);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement