TC2.0编的汉诺塔程序
我是个新手很想搞懂这个小游戏的每一步运行过程,请大家指教(每一步越祥细越好)
汉诺塔程序注释
#include<stdio.h>
void movedisc(unsigned n,char fromneedle,char toneedle,char usingneedle);
int i=0;
void main()
{
unsigned n;
printf("please enter the number of disc:");
scanf("%d",&n); /*输入N值*/
printf("\tneedle:\ta\t b\t c\n");
movedisc(n,'a','c','b'); /*从A上借助B将N个盘子移动到C上*/
printf("\t Total: %d\n",i);
}
void movedisc(unsigned n,char fromneedle,char toneedle,char usingneedle)
{
if(n>0)
{
movedisc(n-1,fromneedle,usingneedle,toneedle);
/*从fromneedle上借助toneedle将N-1个盘子移动到usingneedle上*/
++i;
switch(fromneedle) /*将fromneedle 上的一个盘子移到toneedle上*/
{
case 'a': switch(toneedle)
{
case 'b': printf("\t[%d]:\t%2d.........>%2d\n",i,n,n);
break;
case 'c': printf("\t[%d]:\t%2d...............>%2d\n",i,n,n);
break;
}
break;
case 'b': switch(toneedle)
{
case 'a': printf("\t[%d]:\t%2d<...............>%2d\n",i,n,n);
break;
case 'c': printf("\t[%d]:\t %2d........>%2d\n",i,n,n);
break;
}
break;
case 'c': switch(toneedle)
{
case 'a': printf("\t[%d]:\t%2d<............%2d\n",i,n,n);
break;
case 'b': printf("\t[%d]:\t%2d<........%2d\n",i,n,n);
break;
}
break;
}
movedisc(n-1,usingneedle,toneedle,fromneedle);
/*从usingneedle上借助fromneedle将N-1个盘子移动到toneedle上*/
}
}
搜索更多相关的解决方案:
汉诺塔
----------------解决方案--------------------------------------------------------
搜论坛,这样的帖子里面有很多。
----------------解决方案--------------------------------------------------------
!
----------------解决方案--------------------------------------------------------
soft_wind 你好!
我需要详细到最好每一句的那种注释!
这里有吗??请指教@!
----------------解决方案--------------------------------------------------------
呵呵,楼上的想要详细到那种程度的话,我建议楼上的先去看看书,递归书上还是讲得很多的.
----------------解决方案--------------------------------------------------------
呵您说的很对!
可是小弟真的急用!
求您帮忙!谢谢!
----------------解决方案--------------------------------------------------------