fork(1) download
  1. #include <stdio.h>
  2. void hanoi(int n, char from, char temp, char to) {
  3. if (n == 1) {
  4. printf("将圆盘 %d 从 %c 移动到 %c\n", n, from, to);
  5. return ;
  6. }
  7. hanoi(n-1,from,to,temp);
  8. printf("将圆盘 %d 从 %c 移动到 %c\n", n, from, to);
  9. hanoi(n-1,temp,from,to);
  10. }
  11. int main() {
  12. int n;
  13. printf("请输入汉诺塔的圆盘数量:");
  14. scanf("%d", &n);
  15. hanoi(n, 'A', 'B', 'C');
  16. return 0;
  17. }
Success #stdin #stdout 0s 544KB
stdin
3
stdout
请输入汉诺塔的圆盘数量:将圆盘 1 从 A 移动到 C
将圆盘 2 从 A 移动到 B
将圆盘 1 从 C 移动到 B
将圆盘 3 从 A 移动到 C
将圆盘 1 从 B 移动到 A
将圆盘 2 从 B 移动到 C
将圆盘 1 从 A 移动到 C