2012年11月24日 星期六

數列搜尋

有一數列0、1、3、8、21、......,輸入某一順序職,輸出該順序值得實際值,若順序為0代表結束。
輸入範例:
2
4
6
0
輸出範例:
1
8
55

2 則留言:

  1. Dim number, ans
    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
    Input #1, num
    If num = 0 Then Exit Do
    ReDim number(num)
    Call rr(num)
    Print #2, ans
    Loop
    Close #2
    Close #1
    End
    End Sub

    Public Function rr(ByVal k As Integer)
    If k = 1 Then rr = 0
    If k = 2 Then rr = 1
    If k > 2 Then rr = rr(k - 1) * 3 - rr(k - 2)
    ans = rr
    End Function

    回覆刪除
  2. Dim ans As Integer
    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, num
    Call a(Val(num))
    Print #2, ans
    Loop
    Close #2
    Close #1
    End
    End Sub

    Function a(b)
    If b = 0 Then End
    If b = 2 Then a = 1
    If b = 3 Then a = 3
    If b > 3 Then a = a(b - 1) * 3 - a(b - 2)
    ans = a
    End Function

    回覆刪除