vb编程求斐波那契数列(vb实现斐波那契)
VB编程:1.求斐波那契数列2.利用单选框数组改变字体大小
Option Base 1
Dim a()
Private Sub Command1_Click()
n = Val(InputBox("输入N的值", "求斐波那契数列第N项的值"))
ReDim a(n)
a(1) = 1: a(2) = 1
For i = 3 To n
a(i) = a(i - 1) + a(i - 2)
Next
Print "第"; n; "项的值是:"; a(n)
End Sub
vb求斐波那契数列
求采纳~
Option Base 1
Private Sub Command1_Click()
Dim fibo()
i = Val(InputBox("输入i的值", "求斐波那契数列第i项的值"))
ReDim fibo(i)
fibo(1) = 1: fibo(2) = 1
For n = 3 To i
fibo(n) = fibo(n - 1) + fibo(n - 2)
Next
Print "第"; i; "项的值是:"; fibo(i)
End Sub
求个算斐波那契数列的VB程序
lstfib 是列表框的名称;cmdfib是命令按钮名称;Private Sub cmdfib_Click()
Open App.Path + "\FB.dat" For Output As #1
Dim fib(0 To 9) As Integer
Dim avger As Double, sum As Double
fib(0) = 1: fib(1) = 1
Dim i As Integer
For i = 2 To 9 Step 1
fib(i) = fib(i - 1) + fib(i - 2)
Next i
lstfib.AddItem "下面是Fib数列前10项:", 0
For i = 0 To 9 Step 1
lstfib.AddItem "" fib(i), i + 1
Print #1, fib(i)
Next i
Close #1
For i = 0 To 9 Step 1
sum = sum + Val(lstfib.List(i + 1))
Next i
avger = sum / 10
lstfib.AddItem "前10项的和是:" sum, lstfib.ListCount
lstfib.AddItem "前10平均数是:" avger, lstfib.ListCount
End Sub
For i = 2 To 9 Step 1
fib(i) = fib(i - 1) + fib(i - 2)
Next i
lstfib.AddItem "下面是Fib数列前10项:", 0
For i = 0 To 9 Step 1
lstfib.AddItem "" fib(i), i + 1
Print #1, fib(i)
Next i
Close #1
End Sub
使用VB如何编写程序求第n项斐波那契数列的值
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 I As Long
I = Val(inputbox("n=?"))
Print "第 " I " 项斐波那契数列值为:" F(i)
End Sub
VB斐波那契数列
原来的是很废的写法
Function f(ByVal n As Double) As Double ? ? '定义求第n项值的函数
? ?If n = 1 Then
? ? ? ?f = 0
? ?ElseIf n = 2 Then
? ? ? ?f = 1
? ?Else
? ? ? ?f = f(n - 1) + f(n - 2)
? ?End If
End Function
改成
Function?f(ByVal?n?As?Double)?As?Double?????'定义求第n项值的函数
????dim?a(100)?as?integer
????a(1)=0
????a(2)=1
????for?i=3?to?n
????????a(i)=a(i-1)+a(i-2)
????next
????f=a(n)
End?Function