| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 773 人关注过本帖
标题:[求助]关于函数递归调用问题(Hanoi塔问题)
只看楼主 加入收藏
lgywan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-12-22
收藏
 问题点数:0 回复次数:4 
[求助]关于函数递归调用问题(Hanoi塔问题)
中间有一个Hanoi过程:
sub hanoi(n as integer, one as string,two as string,three as string)
if n=1 then
call hmove(one,three)
else
call hanoi(n-1,one,three,two)/n被减到1,执行homve(one,three)完毕后执行ENDSUB后为什么还要跳到ENDIF并n+1?   
call hmove(one,three) /当n被加満到原值时,为什么会跳到call hmove(one,three)执行?也就是为什么会从endsub跳         / 到这句?
call hanoi(n-1,two,one,three)
endif
endsub
为什么执行完endif endsub 后会马上跳到call homve(one,three)执行,应该是跳回主程序的呀,为什么?
搜索更多相关主题的帖子: Hanoi 递归 three 函数 one 
2005-12-26 20:35
lgywan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-12-22
收藏
得分:0 

难倒在这里没有人知道吗?

2005-12-27 09:01
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
代码不完全。。。

天津网站建设 http://www./
2005-12-27 10:16
lgywan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-12-22
收藏
得分:0 

Private Sub Command1_Click()
Dim m As Integer
List1.Clear
m = Val(Text1.Text)
Call hanoi(m, "a", "b", "c")
End Sub

Sub hmove(getone As String, putone As String)
Dim temp As String
temp = getone & "===>" & putone & " "
List1.AddItem temp
End Sub

1. Sub hanoi(n As Integer, one As String, two As String, three As String)
2. If n = 1 Then
3. Call hmove(one, three)
4. Else
5. Call hanoi(n - 1, one, three, two)
6. Call hmove(one, three)
7. Call hanoi(n - 1, two, one, three)
8. End If
9. End Sub

  当n的值为3,传入1时,执行2、4、5后又回到1,这样当n的值为1时跳到过程 hmove .执行完hmove后跳到8、9
执行,为什么当执行完8、9时n的值会自动加1?为什么还会马上跳回到6执行?当执行完6程序从homve过程中跳回
时执行7,重新又回到1、2、3开始执行跳到hmove过程后回来执行7,程序跳到1、2、3、hmove过程,完了后为什么
一直在8、9(end if ;end sub)之间执行,每执行一次n就加1,当n=3时又跳到6重新执行(好像程序又进入新的循环)???为什么会这样跳来跳去的???到底这程序在按照什么来跳???

2005-12-27 17:24
lgywan
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-12-22
收藏
得分:0 

没有人知道???

2006-01-01 18:37
快速回复:[求助]关于函数递归调用问题(Hanoi塔问题)
数据加载中...
 
   



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

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