16.活動課上,n個學生要兩兩組隊進行拔河比賽,要求每個小組總體重不超過120kg,小林想知道最多可以組多少個隊伍,并希望得到一組可行的組隊方案。于是設計如下界面的程序,在文本框Text1中輸入n個學生的體重(數字之間用逗號隔開),單擊“隊伍”按鈕( Command1)后,標簽( Labell)顯示最多可組隊伍數量,同時在列表框(List1)輸出方案。 實現上述功能的VB程序如下,請在畫線處填入合適代碼。 Dim n As Integer'n用于記錄數字個數 Dm a( 1 To 50)As Integer Private sub Cortmandl_Click( ) Dim s As String Dim i As Integer,j As Integer,t As Integer Dim cnt As Integer Dim st As Integer,ed As Integer s=Text1.Text makedata(s)'將輸入的體重分別存入數組a中,過程代碼略! For i=I To n-1 For j=n To i+1 Step-1 If(1)Then'從大到小排序 1=a(j):a (j)=a(j-1):a(j-1)=t End If Next j Next i '下列程序段實現計算最多可組隊伍 cnt=0 st=1:ed=n Do While st<ed If a(st)+a(ed)<=120 Then List1.Additem str(a(st))+″和″+Str(a(ed))+″組隊″ cnt=cnt+1 st=st+1 (2) Else st=st+1 End If Loop Labell.Caption=″最多可以組″+Str(cnt)+″組隊伍″ End sub
17.設計VB應用程序,輸入一段英文,統計單詞個數和單詞平均長度,該段英文中可能出現符包括大小寫英文字母、數字、特殊字符,標點符號(逗號,句號,空格,感嘆號,問號,分號,雙引號),大小寫字母和數字算單詞組成部分,其他都不計算,程序界面如下。 實現上述功能的VB程序如下,請在點線處填入合適代碼。 Private Sub Command Click( ) Dim s As String Dim n As Integer Dim cnt As Integer,i As Integer Dim num As Single,ch As Single,ave As single s=Text1.Text n=Len(s) num=0 ch=0 cnt=0 ave=0 For i=1 To n c=Mid(s,i,1) If(1)Then cnt=cnt+1 Elself c>=″A″And e<=″Z″Then cnt=cnt+1 EiseIf c>=″0″And c<=″9″Then cnt=cnt+l Else If cnt>0 Then num=num+1 ch=ch+cnt (2) End If End If Next i If cnt>1 Then nun=nun+1 ch=ch+cnt End If Label1.Caption=″總共有″+Str(num)+″個單詞,平均長度為″+Str(ave) End Sub