早教吧 育儿知识 作业答案 考试题库 百科 知识分享

有一个数列前两项为1,从第三项开始,每一项均为前两项之和,求次数列第20个数.(用递归实现)OptionExplicitFunctionfact(n)AsDoubleDima(n)AsSingle,i%Fori=1To2a(i)=1NextiFori=3To20a(i)=a(i-

题目详情
有一个数列前两项为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
▼优质解答
答案和解析
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