【加试题】小明设计了一个查找数据的程序:在一组升序的数列当中,查找不小于k的最小数的位置,如果该值存在,则返回其第一次出现的位置,如果不存在则返回0。
程序界面如图所示:
(1)
若在Text1中输入“8”,Text2,Text3输出的分别为。
(2)
请在画线处填入合适的代码。
Dim a(1 To 10)As Integer
Function
find(L As Integer,R As Integer,key As
Integer)As Integer
If L>R Then
find=0:Exit Functionhp
ElseIf
a(L)>=key Then
find=L:Exit Function
Else
①
If a(M)<key Thenhp
find=find(M+1,R,key)
ElseIf
② Then
find=find(L,M-1,key)
Else
find=M
End If
End
If
End Function
Private Sub
Command1 Click()
Dim k
As Integer
Dim P As Integer
k=Val(Text1.Text)
③
Text2.Text=a(P)
Text3.Text=Str(P)
If p=0 Then
Text2.Text=“无”
End If
End Sub
Private Sub
Form Load()
a(1)=3:a(2)=3:a(3)=3:a(4)=4:a(5)=7:a(6)=7
a(7)=10:a(8)=13:a(9)=19:a(10)=21
For i=1
To 10
List1.AddItem Str(a(i))
Next i
End Sub
① ② ③
答案: 【1】10 7
【1】M=(L+R)\2或M=(L+R+1)\2或其他等价答案【2】a(M-1)>=key【3】p=find(1,10,k)