用VB 求关于斐波纳契数列的问题

来源:百度知道 编辑:UC知道 时间:2024/06/15 11:46:00
窗体上有两个按钮,要求编程斐波纳契求N项值,要求运算是单击C1输入N 值.单击C2显示结果.
说明:斐波纳契数列地一项为0.第二项为1 以后每项为前2项的和

Dim n As Integer
Private Sub Command1_Click()
n = InputBox("请输入N值")
End Sub

Private Sub Command2_Click()
Dim f1 As Long
Dim f2 As Long
Dim f3 As Long
f1 = 0
f2 = 1
For i = 3 To n
f3 = f1 + f2
f1 = f2
f2 = f3
Next i
Print "第" & n & "项的值为" & f3
End Sub

dim n as integer
dim s
n=msgbox("请输入 N 的值")
ReDim Preserve s(1 to 2)
s(1)=0:s(2)=1
for i=3 to n
ReDim Preserve s(i)
s(i)=s(i-1)+s(i-2)
next i
Print s(n)