2012年11月20日 星期二

愛有等差


內容 :
子曰:「親親而仁民,仁民而愛物。」此話展現出儒家愛有等差的精神。具有懷疑精神的歷史學家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

3 則留言:

  1. Dim mycount, temp 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
    Do
    Input #1, mycount
    If mycount = 0 Then Exit Do
    For i = 1 To mycount
    Input #1, temp
    List1.AddItem temp + 100000
    Next
    If Abs(List1.List(mycount - 1) - List1.List(mycount - 2)) = Abs(List1.List(mycount - 2) - List1.List(mycount - 3)) Then Print #2, "yes"
    If Abs(List1.List(mycount - 1) - List1.List(mycount - 2)) <> Abs(List1.List(mycount - 2) - List1.List(mycount - 3)) Then Print #2, "no"
    List1.Clear
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim s As Boolean
    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, x
    If x = 0 Then Exit Do
    Line Input #1, n
    a = Split(n, " ")
    For i = 0 To x - 1
    For j = 0 To x - 2
    If a(j) > a(j + 1) Then
    t = a(j)
    a(j) = a(j + 1)
    a(j + 1) = t
    End If
    Next
    Next
    m = a(1) - a(0)
    s = True
    For i = 0 To x - 2
    If a(i + 1) - a(i) <> m Then s = False: Exit For
    Next
    If s = True Then Print #2, "Yes" Else Print #2, "No"

    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Dim x() As Integer
    Dim lv As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    lv = True
    Do
    Input #1, n
    If n = 0 Then Exit Do
    ReDim x(Val(n - 1))
    For i = 0 To n - 1
    Input #1, x(i)
    Next
    For i = 0 To UBound(x) - 1
    For j = i + 1 To UBound(x)
    If Val(x(i)) > Val(x(j)) Then
    a = x(i)
    x(i) = x(j)
    x(j) = a
    End If
    Next
    Next
    For i = 0 To n - 4
    If x(i) - x(i + 1) <> x(i + 2) - x(i + 3) Then lv = False: Exit For
    Next
    If lv Then
    Print #2, "Yes"
    Else
    Print #2, "No"
    End If
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除