2011年4月11日 星期一

第3題.新費氏函數

說明:新費氏函數的前3項,分別為1,1,2,從第4項起,每項內容是前3項的總和。
在輸入檔 in3.txt 中,只有1個數字n,n>0,請計算第n項的新費氏函數後,輸出到out3.txt

輸入範例: in3.txt
7
輸出範例:
24

5 則留言:

  1. Dim d(1000000)
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out3.txt" For Output As #2
    Open App.Path & "\in3.txt" For Input As #1

    Input #1, n
    d(0) = 0
    d(1) = 1
    d(2) = 1
    For i = 3 To n
    dd = p(d(i - 2), d(i - 3))
    d(i) = p(d(i - 1), dd)
    Next i
    Print #2, d(n)
    Close #1
    Close #2
    End
    End Sub
    Function p(ByVal a1, ByVal b1)
    If Len(a1) < Len(b1) Then Call cz(a1, b1)
    If Len(a1) > Len(b1) Then Call cz(b1, a1)
    Lena1 = Len(a1)
    lenb1 = Len(b1)
    For i = Len(a1) To 1 Step -1
    m1 = Mid(a1, i, 1)
    n1 = Mid(b1, i, 1)
    o1 = Val(m1) + Val(n1)
    X1 = o1 Mod 10
    z1 = X1 + Y1
    Y1 = o1 \ 10
    c1 = z1 & c1
    Next i
    If Y1 > 0 Then c1 = Y1 & c1
    p = c1
    End Function
    Sub cz(a4, b4)
    Do
    a4 = "0" & a4
    If Len(a4) = Len(b4) Then Exit Do
    Loop
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2

    Input #1, N
    a = 1: b = 1: c = 2

    For i = 1 To N - 3
    tmp = a + b + c
    a = b: b = c: c = tmp
    Next

    Print #2, c

    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Dim N As Byte

    Open App.Path & "\in3.txt" For Input As #1
    Open App.Path & "\out3.txt" For Output As #2
    Input #1, N
    List1.AddItem 1
    List1.AddItem 1
    List1.AddItem 2
    For i = 4 To N
    List1.List(i - 1) = Val(List1.List(i - 2)) + Val(List1.List(i - 3)) + Val(List1.List(i - 4))
    If i = N Then ans = List1.List(i - 1)
    Next i

    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. 緣尉 Arro 佑 各位好
    你們的程式都對呦!^.<
    不錯呦!

    回覆刪除
  5. 結果,是緣尉的大數加法錯了,連帶著,這個費氏也錯了。
    可惜了。

    回覆刪除