內容 :
子曰:「親親而仁民,仁民而愛物。」此話展現出儒家愛有等差的精神。具有懷疑精神的歷史學家S.A.S.,想要驗證古聖先賢的思考是否在現代尚能應驗。 全台灣有2500萬人左右,而每個人對其他人都有一個喜好的程度,如果這些喜好程度可以排成一等差數列,那我們就說愛有等差。S.A.S.決定隨便抓幾個路人來實驗是否愛有等差。
輸入說明 :
輸入資料中有多組測試資料。 每一組都是由一個整數n (0<n<50001)開始,代表有幾個人。 接下來是n個數字Ki (-1000000001<Ki<1000000001),代表這位路人對n個人分別的喜好程度。如果n=0,就代表輸入結束。
輸出說明 :
如果愛有等差,就輸出Yes,不然輸出No。
範例輸入 :
4
2 3 1 4
4
2 3 1 5
0
範例輸出 :
Yes
No
出處 :
2009 NPSC 高中組決賽
Dim k, m, n, ans As String
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do While Not EOF(1)
Input #1, k
If k <> 0 Then
Line Input #1, m
n = Split(m)
List1.Clear
For i = 0 To UBound(n)
Call DouZero(n(i))
List1.AddItem n(i)
Next
ans = "YES"
Dim C: C = Val(List1.List(1)) - Val(List1.List(0))
For i = 1 To List1.ListCount - 1
If Val(List1.List(i)) - Val(List1.List(i - 1)) <> C Then ans = "NO"
Next
Print #2, ans
End If
Loop
Close
Close
End
End Sub
Function DouZero(a)
Do While Len(a) < 10
a = "0" & a
Loop
End Function
----------
IN:
8
9 7 6 5 4 2 8 3
3
100 300 201
4
5 50 95 140
0
OUT:
YES
NO
YES
Dim X() As Long
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do
Input #1, n
If n = 0 Then Exit Do
ReDim X(n)
For i = 1 To n
Input #1, X(i)
Next i
Call Sort
Diff = X(2) - X(1)
Have_LOVe = True
For i = 1 To (UBound(X) - 1)
If X(i + 1) - X(i) <> Diff Then Have_LOVe = False
Next i
If Have_LOVe = True Then Print #2, "Yes" Else Print #2, "No"
Loop Until n = 0
Close #2
Close #1
End
End Sub
Sub Sort() '遞增排序
For i = 1 To (UBound(X))
For j = i + 1 To UBound(X)
If X(i) > X(j) Then
T = X(i)
X(i) = X(j)
X(j) = T
End If
Next j
Next i
End Sub
----------
IN:
8
9 7 6 5 4 2 8 3
3
100 300 201
4
5 50 95 140
0
OUT:
YES
NO
YES