fork(1) download
  1. char list[5] = {'1' , '2' , '3' , '4' , '5'};
  2.  
  3. main()
  4. {
  5. int n;
  6. scanf("%d", &n);
  7.  
  8. if(n > 0 && n <= 5)
  9. perm(n);
  10.  
  11. return 0;
  12. }
  13.  
  14. typedef struct _PermInfo{
  15. int i;
  16. int j;
  17. struct _PermInfo *parent;
  18. }PermInfo;
  19.  
  20. push(PermInfo **top, int i, int j)
  21. {
  22. PermInfo *temp = (PermInfo *)malloc(sizeof(PermInfo));
  23. temp->i = i;
  24. temp->j = j;
  25. temp->parent = *top;
  26. *top = temp;
  27. }
  28.  
  29. pop(PermInfo **top)
  30. {
  31. PermInfo *temp = (*top)->parent;
  32. free(*top);
  33. *top = temp;
  34. }
  35.  
  36. swap(char *a, char *b)
  37. {
  38. char temp = *a;
  39. *a = *b;
  40. *b = temp;
  41. }
  42.  
  43. perm(int number)
  44. {
  45. int i;
  46. PermInfo *top = 0;
  47.  
  48. push(&top, 0, 0);
  49.  
  50. while(top)
  51. {
  52. if(top->i == number)
  53. {
  54. for(i = 0; i < number; ++i)
  55. printf("%c" , list[i]);
  56. printf( "\n" );
  57. }
  58.  
  59. if(top->j < number)
  60. {
  61. swap(&list[top->i], &list[top->j]);
  62. push(&top, top->i + 1, top->i + 1);
  63. }
  64. else
  65. {
  66. pop(&top);
  67. if(top)
  68. {
  69. swap(&list[top->i], &list[top->j]);
  70. ++top->j;
  71. }
  72. }
  73. }
  74. }
  75.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
Standard output is empty