2012年7月24日 星期二

愛有等差


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

4 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Dim n() As Double
    Dim b As Boolean
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, n1
    ReDim n(n1)
    For i = 1 To n1
    Input #1, x
    n(i) = x
    Next
    For i = 1 To n1
    For j = 1 To n1 - i
    If n(j) > n(j + 1) Then
    a = n(j)
    n(j) = n(j + 1)
    n(j + 1) = a
    End If
    Next
    Next
    If n1 >= 2 Then
    c = n(2) - n(1)
    For i = 1 To n1 - 1
    If n(i + 1) - n(i) = c Then b = True Else b = False: Exit For
    Next
    If b = True Then Print #2, "YES" Else Print #2, "NO"
    End If
    If n1 = 1 Then Print #2, "NO"
    Loop Until n1 = 0
    Close
    Close
    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
    Dim ki() As Double
    Dim t As Boolean
    Do
    Input #1, n
    ReDim ki(n)
    For i = 1 To n
    Input #1, ki(i)
    Next i
    c = 0
    For j = 1 To n
    For k = 1 To n - 1
    If ki(k) > ki(k + 1) Then
    c = ki(k)
    ki(k) = ki(k + 1)
    ki(k + 1) = c
    End If
    Next k
    Next j
    If n >= 2 Then
    b = ki(2) - ki(1)
    t = True
    For i1 = 2 To n - 1
    If ki(i1 + 1) - ki(i1) <> b Then t = False: Print #2, "No": Exit For
    Next i1
    If t = True Then Print #2, "Yes"
    End If
    Loop Until n = 0
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. 小冰好,哲好,
    兩個程式都正確。就是先排序,再看是不是等差。
    小冰的
    If n(i + 1) - n(i) = c Then b = True Else b = False: Exit For
    這一行比較不好,像哲寫的好。
    而哲寫的
    If t = True Then Print #2, "Yes"
    則是小冰的方式,將兩個不同的輸出,放在同一行,在檢查及找錯誤時,容易些。
    都很好,加油。
    還有晟晟呢?
    這些天看人家在奧運的辛苦與風光,你們不想在高職生的奧運-全國技藝競賽-上也一樣發光發熱嗎?
    成功是一天一天的累積的,向前吧,三位選手們。

    回覆刪除
  4. Dim t 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 While Not EOF(1)
    Input #1, x
    If x = 0 Then End
    t = True
    Line Input #1, y
    a = Split(y)
    For i = 0 To UBound(a)
    For j = i To UBound(a)
    If a(i) > a(j) Then
    z = a(i)
    a(i) = a(j)
    a(j) = z
    End If
    Next j
    Next i
    ans = a(UBound(a)) - a(UBound(a) - 1)
    For i = UBound(a) - 1 To 1 Step -1
    If a(i) - a(i - 1) <> ans Then t = False
    ans = a(i) - a(i - 1)
    Next i
    If t = True Then Print #2, "Yes"
    If t = False Then Print #2, "No"
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除