16.活動(dòng)課上,n個(gè)學(xué)生要兩兩組隊(duì)進(jìn)行拔河比賽,要求每個(gè)小組總體重不超過120kg,小林想知道最多可以組多少個(gè)隊(duì)伍,并希望得到一組可行的組隊(duì)方案。于是設(shè)計(jì)如下界面的程序,在文本框Text1中輸入n個(gè)學(xué)生的體重(數(shù)字之間用逗號(hào)隔開),單擊“隊(duì)伍”按鈕( Command1)后,標(biāo)簽( Labell)顯示最多可組隊(duì)伍數(shù)量,同時(shí)在列表框(List1)輸出方案。 實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)?jiān)诋嬀€處填入合適代碼。 Dim n As Integer'n用于記錄數(shù)字個(gè)數(shù) 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)'將輸入的體重分別存入數(shù)組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 '下列程序段實(shí)現(xiàn)計(jì)算最多可組隊(duì)伍 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))+″組隊(duì)″ cnt=cnt+1 st=st+1 (2) Else st=st+1 End If Loop Labell.Caption=″最多可以組″+Str(cnt)+″組隊(duì)伍″ End sub
17.設(shè)計(jì)VB應(yīng)用程序,輸入一段英文,統(tǒng)計(jì)單詞個(gè)數(shù)和單詞平均長(zhǎng)度,該段英文中可能出現(xiàn)符包括大小寫英文字母、數(shù)字、特殊字符,標(biāo)點(diǎn)符號(hào)(逗號(hào),句號(hào),空格,感嘆號(hào),問號(hào),分號(hào),雙引號(hào)),大小寫字母和數(shù)字算單詞組成部分,其他都不計(jì)算,程序界面如下。 實(shí)現(xiàn)上述功能的VB程序如下,請(qǐng)?jiān)邳c(diǎn)線處填入合適代碼。 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)+″個(gè)單詞,平均長(zhǎng)度為″+Str(ave) End Sub