用递归算法编写求斐波那契数列前n项和的程序用function定义函数

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/28 16:46:13
用递归算法编写求斐波那契数列前n项和的程序用function定义函数

用递归算法编写求斐波那契数列前n项和的程序用function定义函数
用递归算法编写求斐波那契数列前n项和的程序
用function定义函数

用递归算法编写求斐波那契数列前n项和的程序用function定义函数
Private Function F(n As Long) As Long
If n > 2 Then
F = F(n - 1) + F(n - 2)
Else
F = 1
End If
End Function
Private Sub Command1_Click()
Dim a As String
Dim i as long
Dim n as long
Dim sum as long
a = InputBox("输入斐波那契数列项数","输入")
n = Clng(a)
Cls
For i=1 to n
sum = sum + F(i)
Next
Print "斐波那契数列前";Cstr(n);"项和:";sum
End Sub