VB中的递归法是怎样的
答案:2 悬赏:60 手机版
解决时间 2021-03-17 17:35
- 提问者网友:两耳就是菩提
- 2021-03-17 13:44
VB中的递归法是怎样的
最佳答案
- 五星知识达人网友:夜余生
- 2021-03-17 15:15
递归法计算斐波那契序列
private sub command1_click()
msgbox adds(10), vbinformation
end sub
private function adds(byval n as long, optional byval a = 0, optional byval b = 0) as variant
if a < 1 then a = cdec(1): b = cdec(2)
if n < 1 then exit function
n = n - 1
adds = a / b + adds(n, b, a + b)
end function
参见我的百度空间,有递归法与非递归法的详细说明
private sub command1_click()
msgbox adds(10), vbinformation
end sub
private function adds(byval n as long, optional byval a = 0, optional byval b = 0) as variant
if a < 1 then a = cdec(1): b = cdec(2)
if n < 1 then exit function
n = n - 1
adds = a / b + adds(n, b, a + b)
end function
参见我的百度空间,有递归法与非递归法的详细说明
全部回答
- 1楼网友:独行浪子会拥风
- 2021-03-17 15:55
Private Sub Form_load()
Dim i As Long,n as integer
n=int(val(inputbox("输入Fibonacci数列的项数")))
For i = 1 To n
Print Fibonacci(i)
Next
End Sub
Private Function Fibonacci(n As Long)As Long
If n = 1 Then
Fibonacci = 1
ElseIf n = 2 Then
Fibonacci = 1
Else
Fibonacci = Fibonacci(n-1)+Fibonacci(n-2)
End If
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯