有一个数列前两项为1,从第三项开始,每一项均为前两项之和,求次数列第20个数。(用递归实现)

Option Explicit
Function fact(n) As Double
Dim a(n) As Single, i%
For i = 1 To 2
a(i) = 1
Next i
For i = 3 To 20
a(i) = a(i - 1) + a(i - 2)
Next i
fact = a(i)
End Function
Private Sub Command1_Click()
Dim n As Integer, m As Double
m = fact(n)
Label1.Caption = m
End Sub
请问那里出现错误,急需解决
Function fact(n) As Double

If n > 3 Then
fact = fact(n - 2) + fact(n - 1)

Else
fact = 1
End If
End Function
Private Sub Command1_Click()
Dim n As Integer, m As Double
n = Val(Text1.Text)
If n < 0 Then
Exit Sub
End If
m = fact(n)
Label1.Caption = m
End Sub

第1个回答  2012-04-13
Function fact(n) As Double

If n > 2 Then
fact = fact(n - 2) + fact(n - 1)

Else
fact = 1
End If
End Function
Private Sub Command1_Click()
Dim n As Integer, m As Double
n = Val(Text1.Text)
If n < 0 Then
Exit Sub
End If
m = fact(n)
Label1.Caption = m
End Sub本回答被提问者和网友采纳
第2个回答  2012-04-13
有一个数列前两项为1,从第三项开始,每一项均为前两项之和,求次数列第20个数

f(n)=f(n-1)+f(n-2)
f(n-1)=f(n-2)+f(n-3)
……
f(3)=f(2)+f(1)
第3个回答  2012-04-13
下标溢出?
DIM a(n+1)