。 '人員編號和筆試成績分別存儲到數(shù)組變量 id 和 score 中,應聘總人數(shù)存儲在變量n中面試比例在文本框Text1中輸入,所輸入比例值必須使面試人數(shù)大于1且小于n,代碼略。 Private Sub Command1_Click ____ Dim i As Integer'j As Integer,st As String,t As Integer For i=1 To n-1'按筆試成績從高到低排序 k=i For j=i+1 To n If
Then k=j Next j If k<>i Then st=id(k):id(k)=id(i):id(i)=st t=score(k):score(k)=score(i):score(i)=t End If Next i num=
j=num+1 Do while j<=n If Then Exit Do j=j+1 Loop For k=
List2.AddItem Str(k)+““+id(k)+““+Str(score(k)) Next k End Sub
(2)請在橫線處填寫適當?shù)拇a。 Const n=16'假設酒店總房間數(shù)為16 Dim b(1 To n) As Integer'b 數(shù)組按房間號順序存儲各房間可入住人數(shù) Function find1(x,y) As Integer '函數(shù) find1(x,y)的作用是從 b(x)到 b(y)中找到值為1的數(shù)組變量的下標,若找不到,返回值為0 End Function Private Sub Command1_Click ____ Dim s As Integer,d As Integer,f as integer,num As Integer,roomnum As Integer Dim minnum as integer'minnum 變量存儲當前最優(yōu)方案中的預訂房間數(shù)目 Dim mindist As Integer'mindist 存儲當前的最小分布區(qū)間長度 '所有房間的可入住人數(shù)按房間號碼順序存儲到數(shù)組b中并顯示在list1中,代碼略 num=Val(Text1.Text) minnum=n:mindist=n:s=0:start=0:f=0:pos=0 For i=1 To n If b(i)<>0 Then j=i:s=0:roomnum=0 Do While s<num And j<=n s=s+b(j) If b(j)>0 Then roomnum=roomnum+1 j=j+1 Loop d=
If s=num Then If d<mindist Or
Then start=i:mindist=d:minnum=roomnum:pos=0 End If ElseIf s>num Then'若可入住人數(shù)超出,在當前區(qū)間內所選房間中取消1個單人間 f=find1(i+1,j-2) If f>0 Then If d<mindist Or d=mindist And roomnum-1<minnum Then start=i:mindist=d:minnum=roomnum-1:pos=f End If End If End If End If Next i If start=0 Then Label2.Caption=“房間數(shù)量不足“ Else List2.AddItem“預訂房間:“+Str(minnum)+“間“ For i=start To
If b(i)>0 And i<>pos Then List2.AddItem“房間號:“+Str(i)+“,入住人數(shù):“+Str(b(i)) End If Next i End If End Sub