注册 登录
编程论坛 新人交流区

Hanoi塔问题

撒哈拉沙漠 发布于 2007-09-28 13:02, 843 次点击


大家给我讲讲红色那段程序的运行吧,有点不明白啊。就按照4的情况来说吧。。。。。
编译运行结果如下:
Input a number:
4

The step to moving 4 diskes:

a-->b

a-->c

b-->c

a-->b

……

#include <stdio.h>
#include <conio.h>
int move(int n,int x,int y,int z)
{
if(n==1)
printf("%c-->%c\n",x,z);
else
{
move(n-1,x,z,y);
printf("%c-->%c\n",x,z);
move(n-1,y,x,z);
}
}

int main(void)
{
int h;
printf("\nInput a number:\n");
scanf("%d",&h);
printf("The step to moving %2d diskes:\n",h);
move(h,'a','b','c');
getch();
return 0;
}

0 回复
1