VB:斐波那契数列第一项是1,第二项是1,用递归算法编写一个程序,求数列前N项的和

2024-12-05 07:18:23
推荐回答(1个)
回答(1):

添加一个文本框输入前N项的N值,再添加一个命令按钮即可
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()
Cls
Dim N As Long, I As Long, sum As Long
sum = 0
N = Val(Text1.Text)
For I = 1 To N
sum = sum + F(I)
Next I
Print "斐波那契数列前" & N & "项的和为:" & sum
End Sub