2012年11月20日 星期二

Eva 的回家作業


內容 :
  Eva的家庭作業裏有很多數列填空練習。填空練習的要求是:已知數列的前四項,填出第五項。因 為已經知道這些數列只可能是等差或等比數列,她決定寫一個程式來完成這些練習。
輸入說明 :
第一行是數列的數目t(0 <= t <= 20)。 以下每行均包含四個整數,表示數列的前四項。 約定數列的前五項均為不大於105的自然數,等比數列的比值也是自然數。
輸出說明 :
對輸入的每個數列,輸出它的前五項。
範例輸入 :
2
1 2 3 4
1 2 4 8
範例輸出 :
1 2 3 4 5
1 2 4 8 16

4 則留言:

  1. Dim mycount, one, two, three, four, five 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
    Input #1, mycount
    For i = 1 To mycount
    Input #1, one, two, three, four
    If three - two = two - one Then Print #2, one & " " & two & " " & three & " " & four & " " & four + (three - two)
    If three / two = two / one Then Print #2, one & " " & two & " " & three & " " & four & " " & four * (three / two)
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. 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
    For i = 1 To n
    Line Input #1, x
    m = Split(x, " ")
    If m(3) - m(2) = m(2) - m(1) Then Print #2, x & " " & Val(m(3)) + Val(m(2)) - Val(m(1))
    If m(3) / m(2) = m(2) / m(1) Then Print #2, x & " " & Val(m(3)) * Val(m(2)) / Val(m(1))
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. 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
    For i = 1 To n
    Line Input #1, x
    m = Split(x, " ")
    If m(3) - m(2) = m(2) - m(1) Then
    Print #2, x & " " & Val(m(3)) + Val(m(2)) - Val(m(1))
    ElseIf m(3) / m(2) = m(2) / m(1) Then
    Print #2, x & " " & Val(m(3)) * Val(m(2)) / Val(m(1))
    End If
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  4. 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
    For i = 1 To n
    Line Input #1, y
    a = Right(y, 1)
    x = Split(y)
    For j = 0 To UBound(x) - 3
    If x(j + 1) - x(j) = x(j + 3) - x(j + 2) Then
    Print #2, y & " " & Val(a) + x(j + 1) - x(j)
    Exit For
    End If
    If x(j + 1) / x(j) = x(j + 3) / x(j + 2) Then
    Print #2, y & " " & a * x(j + 1) / x(j)
    Exit For
    End If
    Next
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除