a(1) | a(2) | a(3) | …… | a(n —2) | a(n—1) | a(n) |
3 | 25 | 38 | …… | 55 | 31 | 12 |
依据对分查找思想,设计一个在数组a中查找数据key的程序。实现该功能的VB程序如下,但加框处代码有错,请改正。
Private Sub Command1_Click()
Const n = 6
Dim a(1 To n)As Integer,flag As Boolean
Dim i As Integer,j As Integer,m As Integer,key As Integer
'读取一组正整数,按上述规则存入数组a中,代码略。
key = Val(Text1. Text)
i = 1
j = (n + 1)\2
flag = False
Do While And Not flag '( 1)
m =(i + j)\2
If key = a(m) Then
flag = True
ElseIf key< a(m) Then
j = m - 1
Else
i = m + 1
End If
Loop
If Not flag And j > 0 Then
m=n-i
If key = a(m) Then flag = True
End If
If flag Then
Text2. Text = Str(m)
Else
Text2. Text = "找不到"
End If
End Sub
同类型试题
y = sin x, x∈R, y∈[–1,1],周期为2π,函数图像以 x = (π/2) + kπ 为对称轴
y = arcsin x, x∈[–1,1], y∈[–π/2,π/2]
sin x = 0 ←→ arcsin x = 0
sin x = 1/2 ←→ arcsin x = π/6
sin x = √2/2 ←→ arcsin x = π/4
sin x = 1 ←→ arcsin x = π/2
y = sin x, x∈R, y∈[–1,1],周期为2π,函数图像以 x = (π/2) + kπ 为对称轴
y = arcsin x, x∈[–1,1], y∈[–π/2,π/2]
sin x = 0 ←→ arcsin x = 0
sin x = 1/2 ←→ arcsin x = π/6
sin x = √2/2 ←→ arcsin x = π/4
sin x = 1 ←→ arcsin x = π/2