鱼C论坛

 找回密码
 立即注册
查看: 1513|回复: 4

[已解决]小白求助 请问hanoi塔的这个函数哪里有问题?

[复制链接]
发表于 2020-10-11 22:00:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>

  2. void hanoi_tower(int n,int ox, int tx, int mx)
  3. {
  4.         if(n<1){
  5.                 printf("Error: n>=1\n");
  6.         }else if (n==1){
  7.                 printf("%d->%d\n",ox,tx);
  8.         }else{
  9.                 hanio_tower(n-1,ox,tx,mx);
  10.                 printf("%d->%d\n",ox,tx);
  11.                 hanoi_tower(n-1,tx,ox,mx);
  12.         }
  13. }
  14. int main(void)
  15. {
  16.         int n;
  17.         printf("Enter the height of the tower");
  18.         scanf("%d",&n);
  19.         hanio_tower(n,1,3,2);
  20.         return 0;
  21. }
复制代码


请问哪里有问题?
最佳答案
2020-10-11 22:25:01
你要弄明白hanoi分三步,后面这三个参数的位置移动不要弄错了
从ox经过mx移动到tx,分三步:
第一步,把前n-1个从ox经过tx移动到mx;
第二步,把最下面的一个从ox移动到tx;
第三步,把n-1个从mx经过ox移动到tx
  1. #include <stdio.h>

  2. void hanoi_tower(int n,int ox, int tx, int mx)
  3. {
  4.         if(n<1){
  5.                 printf("Error: n>=1\n");
  6.         }else if (n==1){
  7.                 printf("%d->%d\n",ox,tx);
  8.         }else{
  9.                 hanoi_tower(n-1,ox,mx,tx);//拼写错误,hanoi_tower写成了hanio_tower;后面的参数位置也写错了,mx和tx写反了
  10.                 printf("%d->%d\n",ox,tx);
  11.                 hanoi_tower(n-1,mx,tx,ox);//后面的参数位置也写错了
  12.         }
  13. }
  14. int main(void)
  15. {
  16.         int n;
  17.         printf("Enter the height of the tower");
  18.         scanf("%d",&n);
  19.         hanoi_tower(n,1,3,2);//拼写错误,hanoi_tower写成了hanio_tower
  20.         return 0;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-11 22:24:30 | 显示全部楼层
你在你写的这个函数里调用这个函数怎么行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 22:25:01 | 显示全部楼层    本楼为最佳答案   
你要弄明白hanoi分三步,后面这三个参数的位置移动不要弄错了
从ox经过mx移动到tx,分三步:
第一步,把前n-1个从ox经过tx移动到mx;
第二步,把最下面的一个从ox移动到tx;
第三步,把n-1个从mx经过ox移动到tx
  1. #include <stdio.h>

  2. void hanoi_tower(int n,int ox, int tx, int mx)
  3. {
  4.         if(n<1){
  5.                 printf("Error: n>=1\n");
  6.         }else if (n==1){
  7.                 printf("%d->%d\n",ox,tx);
  8.         }else{
  9.                 hanoi_tower(n-1,ox,mx,tx);//拼写错误,hanoi_tower写成了hanio_tower;后面的参数位置也写错了,mx和tx写反了
  10.                 printf("%d->%d\n",ox,tx);
  11.                 hanoi_tower(n-1,mx,tx,ox);//后面的参数位置也写错了
  12.         }
  13. }
  14. int main(void)
  15. {
  16.         int n;
  17.         printf("Enter the height of the tower");
  18.         scanf("%d",&n);
  19.         hanoi_tower(n,1,3,2);//拼写错误,hanoi_tower写成了hanio_tower
  20.         return 0;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-11 22:26:40 | 显示全部楼层
无助的骚年 发表于 2020-10-11 22:24
你在你写的这个函数里调用这个函数怎么行

函数调用自己,这叫递归
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-16 21:43:48 | 显示全部楼层
sunrise085 发表于 2020-10-11 22:26
函数调用自己,这叫递归

好的,感谢,还没学,以为不行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-10 14:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表