标题:[求助]函数的递归调用
取消只看楼主
黑飞狐
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2005-11-12
 问题点数:0 回复次数:1 
[求助]函数的递归调用

#include "stdio.h"
void move(char x,char y)
{
printf ("%c-->%c\n",x,y);
}
void hanoi(int n,char one,char two,char three)
{
if(n==1)move(one,three);
else {
hanoi (n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}

main()
{
int m;
printf("input the number lr diskes:");
scanf("d",&m);
printf("the step to moving %3d diskes:\n",m);
hanoi(m,'A','B','C');
}
这是汉诺塔问题。请问有谁能解释一下hanoi函数的具体递归过程吗?我怎么总是看不明白。
void hanoi(int n,char one,char two,char three)
{
if(n==1)move(one,three);
else {
hanoi (n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
这中间到底是如何实现的呢?
即hanoi (n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);

搜索更多相关主题的帖子: 递归 函数 
2006-04-27 14:21
黑飞狐
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2005-11-12
得分:0 
我只是说为什么else {
hanoi (n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
第一个是one ,three,two,而后面的又是two,one,three呢?其作用是什么?

2006-04-27 22:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-60577-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 2.194534 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved