【問題】VB 6.0....費氏數列...(急)


Recommended Posts

最近我們電腦老師又出了一個作業要我們操作....

做一個費氏數列函數

比如: fib(1)=1

fib(2)=1

fib(3)=2

fib(4)=3

fib(5)=5.....等等

沒錯...輸入一個正整數N,就會跑出費氏數列第N項的值

我試了好幾次都不行....

我想這是對我這種很少聽老師上課的人的悲哀吧= =""

希望大大們.....趕快救救我吧T_T

Link to post
Share on other sites

用了2個textbox和1個command

我是用副函式的寫法,過程可能比較粗操

畢竟很久沒寫VB了...

P.S.其實下面的文章就有提到了,不過是用C的語法,可以練習轉換看看

Private Sub Command1_Click()

Dim n As Integer

n = Text1.Text

Text2.Text = fs(n)

End Sub

Function fs(ByVal n As Integer) As Long

If (n = 1 Or n = 2) Then

fs = 1

Else

fs = fs(n - 1) + fs(n - 2)

End If

End Function

Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now