Dim a(1 To 5) As Integer
Dim n, k As Integer
Private Sub Form_Load()
n = Val(Text1.Text): k = Val(Text2.Text)
’随机生成n(n>=2)个不重复的0及以上的数字赋值给数组a,代码略
End Sub
Private Sub Command1_Click()
Dim m, l, r As Integer
For i = 1 To n
For j = 1 To n - i
If a(j) > a(j + 1) Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
l = 1: r = a(n) - a(1)
Do While l <= r
m = (l + r) \ 2
If Then
l = m + 1
Else
r = m - 1
End If
Loop
Label1.Caption = Str(r)
End Sub
Function check(ByVal d As Integer) As Boolean
Dim cow, rgt As Integer
cow = 1
rgt = a(1) + d
For i = 2 To n
If a(i) >= rgt Then
cow = cow + 1
①
End If
Next i
If ② Then
check = True
Else
check = False
End If
End Function
① ②