2012年9月3日 星期一

大數費氏函數


輸入一正整數N,並用大數加法的方式輸出第N項費氏函數。



第一項為 1
第二項為 1
第三項為 2


輸入:
70

輸出:
190392490709135

1 則留言:

  1. Dim a()
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    ReDim a(n)
    a(0) = 0
    a(1) = 1
    a(2) = 1
    a(3) = 2
    For i = 4 To n
    If Len(a(i - 1)) > Len(a(i - 2)) Then
    Do
    a(i - 2) = "0" & a(i - 2)
    Loop Until Len(a(i - 1)) = Len(a(i - 2))
    End If
    For j = Len(a(i - 1)) To 1 Step -1
    d = Val(Mid(a(i - 1), j, 1))
    b = Val(Mid(a(i - 2), j, 1))
    c = d + b + e
    e = 0
    If c >= 10 And j <> 1 Then
    e = c \ 10
    c = c Mod 10
    End If
    a(i) = c & a(i)
    Next
    Next
    Print #2, a(n)
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除