鱼C论坛

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

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

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

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

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

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

  4. int m;

  5. printf("input the number of diskes:");
  6. scanf("%d", &m);
  7. printf("The step to moveing %d diskes:\n", m);

  8. hanoi(m, 'A', 'B', 'C');
  9. }

  10. void hanoi(int n, char one, char two, char three)
  11. /* 定义hanoi函数, 将n个盘从one座借助two座,移到three座 */
  12. {
  13. void move(char x, char y); /* 对move函数的声明 */
  14. if( n==1 )
  15. {
  16. move(one, three);
  17. }
  18. else
  19. {
  20. hanoi(n-1, one, three, two);
  21. move(one,three);
  22. hanoi(n-1,two,one,three);
  23. }
  24. }

  25. void move(char x, char y) /* 定义move函数 */
  26. {
  27. printf("%c-->%c\n", x, y);
  28. }

复制代码
这是汉诺塔的步骤代码,谁来解析一下
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-7-31 09:50:43 | 显示全部楼层
  1. hanoi(n-1, one, three, two);
  2. move(one,three);
  3. hanoi(n-1,two,one,three);
复制代码
可以借助空座three将盘子上的n-1个盘子从one移动到two;将one上最后一个盘子移动到three,one变成空座;借助空座one,将two座上的n-1个盘子移动到three。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-7-31 16:01:07 | 显示全部楼层
我已经弄懂了
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-14 07:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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