fork download
  1. #include <stdio.h>
  2. #define SIZE 100
  3.  
  4. int n,
  5. sol[SIZE],
  6. used[SIZE];
  7.  
  8. void perm(int level ) {
  9.  
  10. if(level == n + 1) {
  11.  
  12. for(int i = 1; i <= n; i++) {
  13.  
  14. printf("%d ", sol[i]);
  15. }
  16.  
  17. printf("\n");
  18.  
  19. } else {
  20.  
  21. for(int i = 1; i <= n; ++i) {
  22.  
  23. if(!used[i]) {
  24.  
  25. sol[level] = i;
  26.  
  27. used[i] = 1;
  28.  
  29. perm(level+1);
  30.  
  31. used[i] = 0;
  32. }
  33. }
  34. }
  35. }
  36.  
  37. int main(int argc, char const *argv[]) {
  38.  
  39. printf("n=");
  40. scanf("%d",&n);
  41. perm(1);
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5288KB
stdin
3
stdout
n=1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1