請設計一個程式,能在一個數列中,找出相鄰兩數的最小距離。例如,有一個數列為8,20,27,17,13,28,35,31,8與20的距離為12;20與27的距離為7,與前一個例子相比,其距離較小。
輸入說明:輸入之奇數列為下一行數列中的個數,偶數列為數列資料,代表一組測試資料。每個數字與數字間的區隔為一個空白符號,當奇數列為0時表示結束。(請參照輸入範例)
輸入範圍:每個數列最少有2個數字,最多不超過100個。每個數列中的數字皆大於0,小於1000,且不重覆。
輸入範例:in.txt
8
8 20 27 17 13 28 35 31
15
13 14 55 21 66 72 23 73 1 2 88 83 84 24 7
0
輸出說明:每組測試的數列皆要輸出最小距離。(請參照輸出範例)
輸出範例:out.txt
4
1
Dim mycount, min As Integer
回覆刪除Dim temp 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
Input #1, mycount
min = 9999
If mycount = 0 Then Exit Do
Input #1, temp
strr = Split(temp, " ")
For i = 0 To mycount - 2
If Abs(strr(i + 1) - strr(i)) < min Then min = Abs(strr(i + 1) - strr(i))
Next
Print #2, min
Loop
Close #2
Close #1
End
End Sub
Dim lon 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
lon = 1000
Input #1, n
If n = 0 Then Exit Do
Line Input #1, n
n = Split(n)
For i = 0 To UBound(n) - 1
If n(i) - n(i + 1) > 0 Then
If n(i) - n(i + 1) < lon Then lon = n(i) - n(i + 1)
Else
If n(i + 1) - n(i) < lon Then lon = n(i + 1) - n(i)
End If
Next
Print #2, lon
Loop
Close #2
Close #1
End
End Sub