fork(1) download
  1. #include <stdio.h>
  2. void hanoi(int n, char from, char temp, char to) {
  3. // 终止条件:只有1个圆盘时,直接从源柱移到目标柱
  4.  
  5. // 步骤1:将n-1个圆盘从from移到temp(用to作为辅助)
  6.  
  7. // 步骤2:将第n个圆盘从from移到to
  8.  
  9. // 步骤3:将n-1个圆盘从temp移到to(用from作为辅助)
  10.  
  11. }
  12. int main() {
  13. int n;
  14. printf("请输入汉诺塔的圆盘数量:");
  15. scanf("%d", &n);
  16. // 调用递归函数,初始柱子为A,辅助为B,目标为C
  17. hanoi(n, 'A', 'B', 'C');
  18. return 0;
  19. }
  20.  
Success #stdin #stdout 0s 5352KB
stdin
5
stdout
请输入汉诺塔的圆盘数量: