fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 3
  4. char tower[N][10];
  5. int towerLength;
  6. int isComplete()
  7. {
  8. for (int i = 1; i < N; ++i) {
  9. if (strlen(tower[i]) == towerLength)
  10. return 1;
  11. }
  12. return 0;
  13. }
  14.  
  15. void swap(char *p1, char *p2)
  16. {
  17. int t;
  18. t = strlen(p1);
  19. if (t)--t;
  20. p1 += t;
  21. t = strlen(p2);
  22. if (t)--t;
  23. p2 += t;
  24. if (*p1 == 0) {
  25. *p1 = *p2;
  26. *p2 = '\0';
  27. return;
  28. }
  29. if (*p2 == 0) {
  30. *p2 = *p1, *p1 = '\0';
  31. return;
  32. }
  33. if (*p1 < *p2) {
  34. *(p2 + 1) = *p1;
  35. *p1 = '\0';
  36. return;
  37. } else {
  38. *(p1 + 1) = *p2;
  39. *p2 = '\0';
  40. return;
  41. }
  42. }
  43.  
  44. int main(void)
  45. {
  46. int cycle = 0;
  47. printf("towerLength = ");
  48. scanf("%d",&towerLength);
  49. for (int i = 0; i < N; ++i)
  50. for (int j = 0; j < sizeof(tower) / sizeof(tower[0]); ++j)tower[i][j] = '\0';
  51. for (int i = 0; i < towerLength; ++i) tower[0][i] = '0' + towerLength - i;
  52. while (1) {
  53. swap(tower[(cycle + 1) % N], tower[(cycle + 2) % N]);
  54. ++cycle;
  55. cycle %= N;
  56. for (int i = 0; i < N; ++i)printf("%d:%-12s", i, tower[i]);
  57. // for (int i = 0; i < N; ++i)printf("%-12s", tower[i]);
  58. printf("\n");
  59. if (isComplete())break;;
  60. }
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 2252KB
stdin
4
stdout
towerLength = 0:4321        1:            2:            
0:432         1:            2:1           
0:43          1:2           2:1           
0:43          1:21          2:            
0:4           1:21          2:3           
0:41          1:2           2:3           
0:41          1:            2:32          
0:4           1:            2:321         
0:            1:4           2:321         
0:            1:41          2:32          
0:2           1:41          2:3           
0:21          1:4           2:3           
0:21          1:43          2:            
0:2           1:43          2:1           
0:            1:432         2:1           
0:            1:4321        2: