16.【加試題】小李基于冒泡排序算法編寫了一個VB程序,功能如下:在文本框Text1中顯示排序前的數(shù)據(jù),單擊“排序”按鈕Command1,在文本框Text2中顯示剔除重復(fù)數(shù)據(jù)后的升序排序結(jié)果。程序運(yùn)行界面如圖所示。 實(shí)現(xiàn)上述功能的VB程序如下,但加框處代碼有錯,請改正。 Const n=10 Dim a(1To n) As Integer Private Sub Command1_Click ( ?。?br />Dim i As Integer,j As Integer,t As Integer Dim bottom As Integer '獲取排序前數(shù)據(jù)依次存儲在數(shù)組a中,并在文本框Text1中顯示。代碼略 bottom=n i=1 Do While i<=bottom-1 For j=bottom To i+1Step-1 If Then t=a(j):a(j)=a(j-1):a(j-1)=t ElseIf a(j)=a(j-1)Then'相鄰兩個數(shù)據(jù)相等,進(jìn)行剔除處理 bottom=bottom-1 End If Next j i=i+1 Loop Text2.Text=““ For i=1To bottom Text2.Text=Text2.Text+Str(a(i)) Next i End Sub。
。 (2)請?jiān)跈M線處填入合適的代碼。 Const n=120 Dim a(1To n) As Integer Dim b(1To n) As Integer Private Sub Command1_Click ( ?。?br />Dim m As Integer,i As Integer Dim Start As Integer'當(dāng)前未分割數(shù)據(jù)的第1個元素下標(biāo) Dim Left As Integer'當(dāng)前未分割數(shù)據(jù)的個數(shù) Dim pa As Integer'數(shù)組a的下標(biāo) Dim pb As Integer'數(shù)組b的下標(biāo) '讀取n個轉(zhuǎn)換前的數(shù)據(jù),依次存儲到a(1)、a(2)、…a(n)中,代碼略 m=8 Start=1 Left=n Do While Left>3 If Left<m*m Then m=
Else pa=Start pb=Start For i=1To m*m b(pb)=a(pa) pb=pb+1 If i Mod m=0Then
Else pa=pa+m End If Next i
Start=Start+m*m End If Loop For i=Start To n b(i)=a(i) Next i '依次輸出轉(zhuǎn)換后數(shù)據(jù)b(1)、b(2)、…b(n)中,代碼略 End Sub。