鱼C论坛

 找回密码
 立即注册
查看: 3162|回复: 2

麻烦分析一下这段小甲鱼的代码,我有点疑惑

[复制链接]
发表于 2011-7-31 08:56:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
void main()
{
void hanoi(int n,char one,char two,char three); /* 对hanoi函数的声明 */

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) 
/* 定义hanoi函数, 将n个盘从one座借助two座,移到three座 */
{
void move(char x, char y); /* 对move函数的声明 */
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) /* 定义move函数 */
{
printf("%c-->%c\n", x, y);
}
这是汉诺塔的步骤代码,谁来解析一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-7-31 09:50:43 | 显示全部楼层
hanoi(n-1, one, three, two);
move(one,three);
hanoi(n-1,two,one,three);
可以借助空座three将盘子上的n-1个盘子从one移动到two;将one上最后一个盘子移动到three,one变成空座;借助空座one,将two座上的n-1个盘子移动到three。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-7-31 16:01:07 | 显示全部楼层
我已经弄懂了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-8 12:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表