汉诺塔理解不了
为什么当输入的数字为偶数时第一个盘子是从a移到b的,为奇数时a移向c #include<stdio.h>void main()
{
void hanoi(int n,char one,char two,char three);
int m;
printf("intput the number of diskes:");
scanf("%d",&m);
printf("The step to moveing %d diskes:\n");
hanoi(m,'A','B','C');
}
void hanoi(int n,char one,char two,char three)
{
void move(char x,char y);
if(n==1) move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}
这个问题的程序 首先你的思考方向就是错的。
你要理解的是什么叫递归,而不是一来就搞个汉诺塔,为什么要这样移又为什么要那样移;翅膀没硬就想飞的道理。
递归简单的示例很多,要想理解递归搞个最简单的示例,走下流程,一切自然而解,然后再来走走汉诺塔的流程,盘子设在最少。 谢谢关心,本问题已找到问题根源解决了,祝你身体健康,万事如意! ba21 发表于 2020-2-22 14:31
首先你的思考方向就是错的。
你要理解的是什么叫递归,而不是一来就搞个汉诺塔,为什么要这样移又为什么要 ...
谢谢关心,本问题已找到问题根源解决了,祝你身体健康,万事如意
页:
[1]