2012年11月20日 星期二

VB101小考 第1次_2nd


山寨版的費氏函數。
  S(1)=10
  S(2)=-3
  S(n)=S(n-1) - S(n-2),當n>=3。
  從檔案(in2.txt)裡讀出單一個n,將S(n)的值,輸出到檔案(out2.txt)。

4 則留言:

  1. Dim num(9999), mycount As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in2.txt" For Input As #1
    Open App.Path & "\out2.txt" For Output As #2
    Input #1, mycount
    num(1) = 10
    num(2) = -3
    For i = 3 To mycount
    num(i) = num(i - 1) + num(i - 2)
    Next
    Print #2, num(mycount)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim s()
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    ReDim s(n)
    s(1) = 10
    s(2) = -3
    For i = 3 To n
    s(i) = s(i - 1) - s(i - 2)
    Next
    Print #2, s(n)
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Bob程式錯誤:壞習慣n(9999)要改 & 題目沒看清楚
    KiKi程式正確

    回覆刪除
  4. 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
    Input #1, c
    ReDim a(c)
    a(1) = 10
    a(2) = -3
    For i = 3 To c
    a(i) = a(i - 1) - a(i - 2)
    Next
    Print #2, a(c)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除