用递归算法编写求斐波那契数列前n项和的程序
收起
知与谁同
2018-07-21 16:07:24
3736
0
1
条回答
写回答
取消
提交回答
-
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
2019-07-17 22:54:41