Advertisement
welleyth

2169. Permutations

Dec 28th, 2020
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. const int N = 10;
  5. int n;
  6.  
  7. bool used[N];
  8.  
  9. int m[N];
  10.  
  11. void rec(int pos)
  12. {
  13.     if(pos == n)
  14.     {
  15.         for(int i = 0 ; i < n ; ++i)
  16.         {
  17.             printf("%d ",m[i]);
  18.         }
  19.         puts("");
  20.  
  21.         return;
  22.     }
  23.     for(int i = 1 ; i <= n ; ++i)
  24.     {
  25.         if(!used[i])
  26.         {
  27.             used[i] = true;
  28.             m[pos] = i;
  29.             rec(pos+1);
  30.             m[pos] = -1;
  31.             used[i] = false;
  32.         }
  33.     }
  34.     return;
  35. }
  36.  
  37. signed main()
  38. {
  39.     scanf("%d",&n);
  40.  
  41.     rec(0);
  42.  
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement