当前位置: 代码迷 >> C语言 >> 汉诺塔问题....不是太明白
  详细解决方案

汉诺塔问题....不是太明白

热度:460   发布时间:2007-04-20 13:22:15.0
汉诺塔问题....不是太明白
#include "stdio.h"
int main(void)
{
void hannoi (int n,char one,char two,char three);
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("the step to moveing %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);;
}

这个是谭浩强 第三版书
P176的例题...但是我写在C-FREE上没编译成功.....而且我看不懂程序是如何运行的
请高手们指点指点.....
搜索更多相关的解决方案: 汉诺塔  char  void  int  

----------------解决方案--------------------------------------------------------

给改了一下,不知道改错了没?
#include "stdio.h"
void hanoi (int n,char one,char two,char three);
int main(void)
{
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("the step to moveing %d diskes:\n",m);
hanoi(m,'A','B','C');
return 0;
}
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);
}


----------------解决方案--------------------------------------------------------
这次是对的....
能麻烦给我解释一下程序是如何运行的吗?我看不很懂
----------------解决方案--------------------------------------------------------
比谭浩强书里
多了个return 0;
是这样吧?
我刚开始学函数,还不太懂
----------------解决方案--------------------------------------------------------
递归本身就不容易懂
函数调用自身时,就把他当成调用其他函数一样理解!不要受自己调用自己这个现象的影响。。。
----------------解决方案--------------------------------------------------------
还是搞不很懂- -递归和以前那些题不一样...那些题想一想就知道程序是怎么运行的
这个想了半天也没明白
----------------解决方案--------------------------------------------------------
能看懂这个不:

int CalAge(int n)
{
if(n==0)
{
return 8;
}
else
{
return CalAge(n-1)+1;
}
}
----------------解决方案--------------------------------------------------------
return 8;的意思是???把8返回到 calage(int n)里?
return calage(n-1)+1 的意思就是返回 calage(n-1)+1的值了?
比如 n=1
calage(int n)
就是1了.....
如果n=0
那就是0
这样理解对不对?
不过我感觉不是很对.....
----------------解决方案--------------------------------------------------------
呵呵,肯定是错的,如果按你这样说,那if(n==0)有什么用呢?
你看看这个:

int CalAge(int n)
{
if(n==0)
{
return 8;
}
else
{
int re= CalAge(n-1)+1;
return re;
}
}

有0到m个人,满足第n人比第n-1年龄大1岁,另外已知第0人年龄是8岁,求出第x个人年龄计算的函数,就是上面的结果


----------------解决方案--------------------------------------------------------
我这也不懂???????????????
----------------解决方案--------------------------------------------------------
  相关解决方案