16.小李基于對分查找算法編寫了一個VB程序,功能如下:在文本框Text1中顯示隨機產生n個整數(n為奇數),單擊“查找”按鈕Command1,在標簽Label1中顯示這n個整數的中位數。(注:中位數是指將這n個數排序之后,排在正中間的數。)程序運行界面如圖所示。 實現上述功能的VB程序如下,但加框處代碼有錯,請改正。 Const n=9 Const m=50 Dim a(l To n) As Integer Private Sub Form_Load ( ) '隨機產生n個[l,m]之間的隨機整數,依次存儲在數組a中,并顯示在文本框Text1中。 '代碼略。 End Sub Private Sub Command1_Click ( ?。?br />Dim low As Integer,high As Integer,mid1 As Integer Dim count As Integer,i As Integer low=1:high=m Do While low<high mid1=(low+high)\2 count=0 For i=1 To n IfThen count=count+1'(1) Next i If count>n\2 Then '(2) Else high=mid1 End If Loop Label1.Caption=Str(high) End Sub
。 (2)請在橫線處填入合適的代碼。 Dim n As Integer,k As Integer Dim a(1 To 100)As Integer '本函數將返回第k次分組信息:根據sleft和smid參數值,返回當前挑選的數據及剩余數據 Function prints(sleft As Integer,smid As Integer) As String '代碼略 End Function Private Sub Command1_Click ( ?。?br />Dim i As Integer,j As Integer,t As Integer Dim top As Integer'當前分組數據的起始位置 Dim bottom As Integer'當前分組數據的結束位置 '從文本框Text1中讀取n個原始數據,依次存儲到a(1)、a(2)、……a(n)中,代碼略 i=1:k=0 top=1
List1.Clear Do While top<=bottom For j=bottom To i+1 Step-1 Ifa(j-l)<a(j)Then t=a(j):a(j)=a(j-1):a(j-1)=t Else If
Then t=a(j):a(j)=a(bottom):a(bottom)=t bottom=bottom-1 End If Next j If i>=bottom Then List1.AddItem prints(top,bottom)'輸出第k組數據及剩余數據 top=