2012年11月20日 星期二

數列的公差或等比

在數列中有等差數列以及等比數列,已知道數列的前四項,請判斷出此數列是等差或者等比數列,並求出公差或等比。

輸入說明:
第一行是數列的數目t(0<= t <=10) 。以下每行均包含四個以空白格開的整數,表示數列的前四項。數列的前四項均為不大於10000的自然數,等比數列的比值也是自然樹。

輸出說明:
對數列的每個數列,輸出兩個欄位,欄位與欄位之間以一個空白隔開,若是等差數列則第一欄輸出A,若為等比數列則第一欄輸出G;第二欄則根據數列的判斷結果,輸出其公差或是等比。

輸入範例:
2
2 4 6 8
2 4 8 16
輸出範例:
A 2
G 2

4 則留言:

  1. Dim mycount, one, two, three, four 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, "A" & two - one
    If three / two = two / one Then Print #2, "G" & two / one
    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, "A" & " " & Val(m(2)) - Val(m(1))
    If m(3) / m(2) = m(2) / m(1) Then Print #2, "G" & " " & 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, "A" & " " & Val(m(2)) - Val(m(1))
    ElseIf m(3) / m(2) = m(2) / m(1) Then
    Print #2, "G" & " " & 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, x
    x = Split(x)
    For j = 0 To UBound(x) - 3
    If x(j + 1) - x(j) = x(j + 3) - x(j + 2) Then
    Print #2, "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, "G " & x(j + 1) / x(j)
    Exit For
    End If
    Next
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除