。 (2)實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)?jiān)跈M線處填入合適的代碼。 Private Sub Command1_Click ____ Dim a(1 To 100)As Integer Dim i As Integer,j As Integer Dim temp As Integer,k As Integer Dim res As Integer,ed As Integer '從文本框Textl中依次讀入整數(shù)存儲(chǔ)到數(shù)組a中,整數(shù)的個(gè)數(shù)存放在變量k中,代碼略. For i=1 To k\2-1'以區(qū)間左邊數(shù)據(jù)為主要關(guān)鍵字升序排序 For j=k\2 Toi+1 Step-1 If ①
Then temp=a(j*2-1):a(j*2-1)=a((j-1)*2-1):a((j-1)*2-1)=temp temp=a(j*2):a(j*2)=a((j-1)*2):a((j-1)*2)=temp End If Next j Next i ed=a(2) res=0 For i=2 To k\2'計(jì)算需要?jiǎng)h除區(qū)間的最小數(shù)量 If ②
Then res=res+1 ed=min(ed,③
) Else ed=a(i*2) End If Next i Text2.Text=Str(res) End Sub Function min(x As Integer,y As Integer)As Integer'函數(shù)功能:返回x與y的最小值 '代碼略 End Function
。 (2)請(qǐng)?jiān)冖佗冖厶幪钌虾线m代碼,實(shí)現(xiàn)程序功能。 Dim n As Integer Dim a(1 To 100)As Integer'a數(shù)組存儲(chǔ)各堆果子重量 Dim b(o To 100)As Integer'b(0)存放a數(shù)組中最小值的下標(biāo),若a(i)是數(shù)組中最大數(shù),則 b(i)的值為-1 Dim flag(1 To 100)As Boolean Private Sub Form_Load ____ 初始化果子的數(shù)量n和每堆果子的重量a(i),并依此顯示在列表框list1中,代碼略 初始化b數(shù)組的值均為-1,flag 數(shù)組的值均為False,代碼略 End Sub Private Sub Command1_Click _____ Dim i As Integer,k As Integer Dim p As Integer,q As Integer,w As Integer p=0 Do While True'生成b數(shù)組,標(biāo)記升序后a(i)的下一個(gè)元素在a數(shù)組中的位置為b(i) For i=1 To n If Not flag(i)Then k=i:Exit For Next i If i=n+1 Then Exit Do i=1 Do While i<=n If Not flag(i) And a(k)>a(i)Then k=i i=i+1 Loop b(p)=k flag(k)=True ①
Loop w=0 p=b(0):q=b(p) Do While b(p)<>-1 a(p)=a(p)+a(q) w=②
If b(q)<>-1 Then b(0)=b(q)Else Exit Do Call sort(p) p=b(0):q=b(p) Loop Text1.Text=Str(w) End Sub Sub sort(p As Integer)'將a(p)插入到合適位置 Dim pl As Integer,ql As Integer pl=b(0) If a(p)<=a(pl)Then b(0)=p:b(p)=pl Else Do While a(p)>a(p1)And b(p1)<>-1 ql=pl:pl=b(pl) Loop If b(p1)=-1 And a(p)>a(p1)Then b(pl)=p:b(p)=-1 Else b(p)=pl:③