求解程序,是在一有序列中查找50 同时统计查找的次数并显示,查找次数 (填入空白处并给出原因)
求解程序,是在一有序列中查找50 同时统计查找的次数并显示,查找次数 (填入空白处并给出原因)
Private Sub Command16_Click()Dim m As Integer, i As Integer, j As Integer, x As Integer, total As Integer, f As BooleanDim a(1 To 8) As Integera(1) =7 : a(4) = 30: a(7) =80 a(2) = 8: a(5) =33: a(8) = 100 a(3) = 10 : a(6) = 50f = False: x = 50: i = 1: j = 8: to_tal = 0Do While _____ And f = False total = total + 1 m =________ If a(m) = x Then f = True Else If x < a(m) Then j = m - 1 Else m = m + 1 End If End IfLoopIf f = True ThenLabel5.Caption = _________Else Label5.Caption = "找不到该数据"End IfEnd Sub
Private Sub Command1_Click()Dim m As Integer, i As Integer, j As Integer, x As Integer, total As Integer, f As BooleanDim a(1 To 8) As Integera(1) = 7: a(4) = 30: a(7) = 80a(2) = 8: a(5) = 33: a(8) = 100a(3) = 10: a(6) = 50f = False: x = 17: i = 1: j = 8: to_tal = 0 Do While total < UBound(a) And f = False total = total + 1 m = total If a(m) = x Then f = True Else If x < a(m) Then j = m - 1 Else m = m + 1 End If End If Loop If f = True Then Label1.Caption = "查找了 " & total & " 次,找到序列为 a(" & m & ")" Else Label1.Caption = "找不到该数据" End IfEnd Sub
[解决办法]
根据代码,应该是有序序列的居中开始查找,最大查找次数可以比上面的代码减半……但是代码又有一个地方不符
If x < a(m) Then
j = m - 1
Else
j = m + 1
End If