2010年3月19日 星期五

數列搜尋

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

6 則留言:

  1. Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Input As #2
    Dim num(20) As Long
    num(1) = 1
    For I = 2 To 20
    num(I) = num(I - 1) * 3 - num(I - 2)
    Next I
    Do
    Input #1, x
    If x = 0 Then Exit Sub
    Print #2, num(x - 1)

    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  2. D im A(100) As Long
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    A(1) = 0: A(2) = 1
    Do While Not EOF(1)
    Input #1, x
    For i = 3 To x
    A(i) = A(i - 1) * 3 - A(i - 2)
    Next i
    If x = "0" Then Exit Sub
    Print #2, A(x)
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  3. Dim a(24) As Long
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a(1) = 0
    a(2) = 1
    For i = 3 To 24
    a(i) = a(i - 1) * 3 - a(i - 2)
    Next i
    Do
    Input #1, x
    If x = 0 Then Exit Do
    Print #2, a(x)
    Loop
    Close #1
    Close #2
    End Sub

    回覆刪除
  4. Y揚、高仔、阿瑋好,
    這題也因為討論過,程式是都沒問題。
    不過,你們也都說月考到了,一次又post這麼多題,什麼時候做啊?
    哈。

    回覆刪除
  5. Dim a(100) As Long
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a(1) = 0: a(2) = 1
    For i = 3 To 25
    a(i) = a(i - 1) + a(i - 2)
    Next i
    Do
    Input #1, x
    If x = 0 Then Exit Do
    c = x * 2 - 1
    Print a(c)
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  6. Dim a(100) As Long
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    a(1) = 0: a(2) = 1
    For i = 3 To 25
    a(i) = a(i - 1) + a(i - 2)
    Next i
    Do
    Input #1, x
    If x = 0 Then Exit Do
    c = x * 2 - 1
    Print #2, a(c)
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除