a(1) |
a(2) |
a(3) |
a(4) |
a(5) |
a(6) |
a(7) |
a(8) |
a(9) |
a(10) |
1 |
10 |
3 |
8 |
5 |
6 |
7 |
4 |
9 |
2 |
依据对分查找思想,设计一个在数组a中查找数据key的程序,实现该功能的VB程序如下,请回答下列问题:
Private Sub Search_Click()
Const n = 10
Dim a(1 To n) As Integer
Dim i As Integer, j As Integer, m As Integer, f As Boolean, key As Integer
‘读取一组正整数,按上述规则存入数组a中。代码略
key = Val (Text1.Text)
If key Mod 2=1 Then i=1 Else i=2
j =n:f= False
Do While i<=j And Not f
If key Mod 2=0 Then
m=(i+j)\2-(i+j)\2Mod2
Else
m= ①
End If
If key = a(m) Then
f = True
ElseIf Then
j=m-2
Else
i= ②
End If
Loop
If f Then Label1.Caption = Str(m) Else Label1.Caption =“不存在”
End Sub
① ②