16.某排序算法思想如下:若有11個(gè)桶,編號(hào)從0~10,隨機(jī)產(chǎn)生多個(gè)整數(shù),每產(chǎn)生一個(gè)整數(shù)時(shí),就在以該整數(shù)位編號(hào)的桶中放一面小旗子,最后只要按順序數(shù)每個(gè)桶中有幾面小旗子,就能得到這幾個(gè)整數(shù)的有序排列.例如2號(hào)桶中有1個(gè)小旗子,表示2出現(xiàn)了一次;3號(hào)桶中有1個(gè)小旗子,表示3出現(xiàn)了一次;5號(hào)桶中有2個(gè)小旗子,表示5出現(xiàn)了兩次;8號(hào)桶中有1個(gè)小旗子,表示8出現(xiàn)了一次,按桶有編號(hào)順序讀出旗子數(shù)量,沒(méi)有旗子的桶略過(guò),得到有序整數(shù)為“2,3,5,5,8”. 為此,小李編寫(xiě)了一個(gè)VB程序,功能如下:運(yùn)行程序,窗體加載時(shí)隨機(jī)產(chǎn)生6個(gè)[0,9]的隨機(jī)整數(shù),并在列表框List1中依次顯示排序前數(shù)據(jù),單擊“排序”按鈕Commandl,在列表框Lixt2中顯示經(jīng)過(guò)上述排序算法后的數(shù)據(jù),運(yùn)行結(jié)果如第如圖2所示. 實(shí)現(xiàn)上述功能 的VB代碼如下,但加框代碼有錯(cuò),請(qǐng)改正. Dim a(10)As Integer’數(shù)組a表示桶的編號(hào),如a(0)表示第0號(hào)桶 Private Sub Commandl-Click( ?。?br /> Dim i As Integer,j As Integer For i=0 to 10 For j=1 to List2.AddItem CStr(i) Next j Next i End Sub Private Sub Form-Load Dim i As Integer Randomize For i=0 to 10’將數(shù)組初始化為0 a(i)=0 Next i For i=1 to 6’產(chǎn)生6個(gè)[0,9]的隨機(jī)數(shù) t=Int(Rnd*10) a(t)=’第t桶中小旗子數(shù)量加1 List1.AddItem CStr(t) Next i End Sub.
. (2)請(qǐng)?jiān)跈M線處填入合適的代碼. Dim ach(1 to 500)As String’存儲(chǔ)各學(xué)生選課編碼 Dim bno(1 to 10)As Integer’存儲(chǔ)各科目編號(hào) Dim bname (1 to 10)As String’存儲(chǔ)各科目名稱 Dim bnum(1 to 10)As Integer’存儲(chǔ)各科目被選次數(shù) Dim bmc(1 to 10)As Integer’存儲(chǔ)各科目被選的名次 Dim n As Integer,num As Integer Private Sub Commandl-Click( ?。?br /> Dim i As Integer,km As String km=“物理化學(xué)生物政治歷史地理技術(shù)“ Dim cn As New ADODB.Connection,rs As New ADODB.Recordset,spl As String cn.Open“provider=Microsoft.jye.ai.4;Data Source=“+App.Path+“\students.jye.ai“ rs.Open“select*from choose“,cn Do Whole Not rs.EOF n=n+1:ach(n)=rs.Fields(“subject“):rs.MoveNext Loop rs.Close cn.Close Set rs=Nothing Set cn=Nothing For i=1 to 7 bno(i)=i bname(i)=Mid(km,(i-1)*2+1,2) List2.AddItem TR(bno(i))+“---“+bname(i) Next i End Sub Private Sub Command2-Click( ?。?br /> Dim i As Integer,j As Integer,k As Integer Dim c As String,per As Integer For i=1 to n If check(ach(i))=True Then For j=1 to 3 c=Mid(ach(i),j,1) bnum(Val(c))=
Next j num=num+1 End If Next i i=1 Do While i<=7’該循環(huán)得到各學(xué)科被選人數(shù)的名次 bmc(i)=1 For j=1 to 7 If bnum(j)>bnum(i) Then
Next j i=i+1 Loop For i=1 to 7 For j=1 to 7 If
Then per=In(bnum(j)*100/num*100)/100 List3.AddItem Str(bno(j))+“---“+Str(per)+“%“ End If Next i End Sub Function check(s As String) As Boolean ’代碼略!若學(xué)生選代碼有效,函數(shù)返回True,否則返回False End Function.