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