谁能帮我解释一下汉诺塔程序问题-回答满意加50分

来源:百度知道 编辑:UC知道 时间:2024/06/05 05:49:04
# include <stdio.h>
void main ()
{
void hanoi (int n,char one ,char two,char three);
int m;
printf ("input the number of diskes:");
scanf ("%d",&m);
printf("the step to moving %d diskes :\n",m);
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);
}
我懂这是递归,但我就是不理解它到底是怎么弄的,我弄标记的那块地方我看不懂,我看了好长时间了,请懂的人好好解释给我听,如果我理解的了的话,我加50分.先谢谢了.

void main ()
{
void hanoi (int n,char one ,char two,char three); //从这里开始给你介绍,主程序程序声明用到了一个HANOI的函数,有4个变量N,ONE,TWO和THREE
int m; //这里定义了一个变量M
printf ("input the number of diskes:"); //这里是提问盘子的数目,即你想要得知移动几个盘子
scanf ("%d",&m); //获得盘子的数目
printf("the step to moving %d diskes :\n",m); //输出字符‘N个盘子的移动STEP’
hanoi(m,'A','B','C'); //调用函数HANOI
}
void hanoi(int n,char one,char two,char three) //这里是HANOI函数的内容
{
void move(char x,char y); //调用MOVE函数
if (n==1)
move (one,three); //如果是1个盘子,则1号柱子上的盘子移动到和3号柱子上
else /*****/
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three); //否则(即不是1个盘子的情况下)移动N-1个盘子的情况
} /******/
}
void move (char x,char y)
{
printf("%c-->%c\n",x,y); //打印盘子的移动路线
}
首先你要明白到底什么是叠代.其实如果真的知道什么是叠代了,你肯定知道这个问题的解的过程
我要解决一个问题有N个步骤,如果我知道一个步骤的过程,那么我就可以由N-