Advertisement
GastonFontenla

Untitled

Oct 6th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #define SB(mask, k) (mask |= (1 << k))
  5. #define TB(mask, k) (mask ^= (1 << k))
  6. #define GB(mask, k) ((bool)(mask & (1 << k)))
  7.  
  8. using namespace std;
  9.  
  10. int n;
  11.  
  12. void generarPermutaciones(int &usado, vector <int> &permutacion)
  13. {
  14.     if(permutacion.size() == n)
  15.     {
  16.         for(int i=0; i<permutacion.size(); i++)
  17.             cout << permutacion[i] << " ";
  18.         cout << endl;
  19.         return;
  20.     }
  21.  
  22.     for(int i=1; i<=n; i++)
  23.     {
  24.         if(GB(usado, i) == false)
  25.         {
  26.             permutacion.push_back(i);
  27.             TB(usado, i);
  28.             generarPermutaciones(usado, permutacion);
  29.             permutacion.pop_back();
  30.             TB(usado, i);
  31.         }
  32.     }
  33. }
  34.  
  35. int main()
  36. {
  37.     cin >> n;
  38.  
  39.     int usado = 0;
  40.     vector <int> permutacion;
  41.  
  42.     generarPermutaciones(usado, permutacion);
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement