15.在 n 個(gè)元素的數(shù)組中,小程通過邊分段、邊合并的方法將 n 個(gè)數(shù)據(jù)處理成一個(gè)非降序數(shù)據(jù)段(a(1)≤a(2)≤a(3)≤……),排序的方法如下: (a)從第 1 個(gè)元素開始,在數(shù)組中依次搜索若干個(gè)非降序數(shù)據(jù)段; (b)將第 1、2 個(gè)非降序數(shù)據(jù)段合并成一個(gè)新的非降序數(shù)據(jù)段,再將第 3、4 個(gè)非降序數(shù)據(jù)段合并成一個(gè)新的非降序數(shù)據(jù)段,依次類推。若非降序數(shù)據(jù)段個(gè)數(shù)為奇數(shù),則最后 1 個(gè)數(shù)據(jù)段不處理。 (c)合并后的數(shù)據(jù)段總數(shù)大于 1 個(gè)時(shí),重復(fù)(a)(b)的操作。 編寫 VB 程序,實(shí)現(xiàn)上述合并排序功能。運(yùn)行程序,在標(biāo)簽 Label1 顯示原始數(shù)據(jù),單擊“合并排序”按鈕 Command1,在列表框 List1 中顯示每次排序后結(jié)果。排序過程及程序運(yùn)行界面如圖 a 所示。請(qǐng)回答下列問題: (1)下列既可作為輸入,也可作為輸出的對(duì)象是
。(單選,填字母:A.Label1/B.Text1/C.List1) (2)為實(shí)現(xiàn)上述功能的 VB 程序如下,請(qǐng)?jiān)跈M線處填入合適代碼。 (3)程序中加框處代碼有錯(cuò),請(qǐng)改正。 Const n=10 Dim a(1 to n)As Integer Private Sub Form_Load ( ) '產(chǎn)生 n 個(gè) 1-100 之間的隨機(jī)數(shù),存放在 a 數(shù)組中,并顯示在標(biāo)簽 Label1 中,代碼略。 End Sub Private Sub Command1_Click ( ) Dim num As Integer'num 表示非降序段數(shù) Dim b(n)As Integer,p1 As Integer,p2 As Integer,t As Integer Dim t1 As Integer,t2 As Integer,i As Integer,j As Integer,k As Integer num=0:p1=1 Do While num<>1 t1=pos(p1) num=num+1
If p2>n Then p1=1 Else t2=pos(p2) num=num+1 i=p1:j=p2:t=1 Do While t<=t1+t2'將兩個(gè)非降序段合并 If j>=p2+t2 Or
Then b(t)=a(i):i=i+1 Else b(t)=a(j):j=j+1 End If t=t+1 Loop For k=1 To p1+p2'將合并后的結(jié)果存入 a 數(shù)組 a(p1+k-1)=b(k) Next k p1= If p1>n Then p1=1:num=0 '將每次的排序結(jié)果顯示在列表框 List1 中,代碼略。 End If Loop End Sub Function pos(low As Integer)As Integer'查找從 low 開始連續(xù)非降序段長度 Dim i As Integer For i=low To n-1 If a(i)>a(i+1)Then Exit For Next i
倍。 (2)根據(jù)上述掃描算法,其 VB 代碼實(shí)現(xiàn)如下,請(qǐng)?jiān)跈M線處填入合適的代碼。 Dim a(0 To 1000)As Integer'存儲(chǔ)原矩陣數(shù)據(jù),按行優(yōu)先存儲(chǔ) Dim b(0 To 1000)As Integer'存儲(chǔ)Z形掃描后數(shù)據(jù) Dim c(0 To 1000)As Integer'存儲(chǔ)行程編碼壓縮后數(shù)據(jù) Dim n As Integer ‘矩陣導(dǎo)入代碼略,以行優(yōu)先存儲(chǔ)在a數(shù)組中,如例子中數(shù)據(jù)存儲(chǔ)順序?yàn)椤?7,45,0,23,0,0…” Private Sub Command2_Click ( ) Dim choice As Integer'1:向右移動(dòng);2:向右上移動(dòng);3向下移動(dòng) 4向左下移動(dòng) Dim row As Integer,col As Integer,i As Integer,j As Integer Dim pre As Integer,count As Integer choice=1:row=0:col=0:i=0 Do While (row<>n-1 Or col<>n-1) b(i)=a(row*n+col):i=i+1 If choice=1 Then
If row=0 Then choice=4 Else choice=2 ElseIf choice=2 Then row=row-1:col=col+1 If
Then choice=1 ElseIf col=n-1 Then choice=3 End If ElseIf choice=3 Then row=row+1 If col=0 Then choice=2 Else choice=4 ElseIf choice=4 Then row=row+1:col=col-1 If row=n-1 Then choice=1 ElseIf col=0 Then choice=3 End If End If Loop b(i)=a(n*n-1):j=0:pre=b(0):count=0 For i=0 To n*n-1'輸出Z形序列,并進(jìn)行行程壓縮 If pre=b(i)Then count=count+1 Else c(j)=pre:c(j+1)=count
pre=b(i):j=j+2 End If Next i c(j)=pre:c(j+1)=count Text1.Text=““ For i=0 To j+1 Text1.Text=Text1.Text+Str(c(i))+“,“ Next i End Sub