Dim a(1 To 100) As Integer
Private Sub Form_Load()
'产生100个重量为10的小球,存储在数组a中
'其中随机选取1个小球的重量改为8,代码略
End Sub
Private Sub Command1_Click()
Dim left As Integer, right As Integer
Dim mid As Integer, count As Integer
Dim s(1 To 2) As Integer
left = 1: right = 100: count = 1
List2.AddItem Str(left) + "-------->" + Str(right)
Do While left<=right
s(1) = 0: s(2) = 0
①
i = left: k = 1
Do While i <= left + mid * 2 - 1
s(k) = s(k) + a(i)
If ② Then k = k + 1
i = i + 1
Loop
If s(1) = s(2) Then
left = left + 2 * mid
ElseIf s(1) < s(2) Then
right = left + mid - 1
Else
left = left + mid
End If
count = count + 1
List2.AddItem Str(left) + "-------->" + Str(right)
Loop
List2.AddItem "经过" + Str(count) + "次后找到这个数"
End Sub
① ②