Problem1:等差數列或等比數列(10%)
在數列中有等差數列及等比數列,若已知數列的前四項,請判斷出此數列是等差或是等比數列,並求出公差或公比。
輸入說明 :
第一行是數列的數目t(0 ≤ t ≤ 10)。以下每行均包含四個以空白隔開的整數,表示數列的前四項。數列的前四項均為不大於10000的自然數,等比數列的比值也是自然數。
輸出說明:
對輸入的每個數列,輸出兩個欄位,欄位與欄位之間以一個空白隔開,若是等差數列則第一欄輸出A,若為等比數列則第一欄輸出G;第二欄則根據數列的判斷結果,輸出其公差或是公比。
輸入範例:
2
2 4 6 8
2 4 8 16
輸出範例:
A 2
G 2
Private Sub Form_Load()
回覆刪除Dim X(4)
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
For j = 1 To 4
Input #1, X(j)
Next j
Y = X(4) - X(3)
If X(3) - X(2) = X(2) - X(1) And X(2) - X(1) = Y Then
Print #2, "A"
Else
Y = X(4) / X(3)
If X(3) / X(2) = X(2) / X(1) And X(2) / X(1) = Y Then Print #2, "G"
End If
Next i
Close #2
Close #1
End
End Sub
Dim s, k1, k2
回覆刪除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, t
For i = 1 To t
Line Input #1, m
s = Split(m)
k1 = s(1) - s(0)
k2 = s(1) / s(0)
If s(UBound(s)) - s(UBound(s) - 1) = k1 Then Print #2, "A " & k1
If s(UBound(s)) / s(UBound(s) - 1) = k2 Then Print #2, "G " & k1
Next
Close
Close
End
End Sub