题型:综合题 题类:常考题 难易度:普通
浙江省十校联盟2020届高三上学期信息技术10月联考试卷
按纪念品价值进行分组,每组总价值不超过限定值w且最多包含两件,要求分组数最少。编写VB程序解决该问题,程序运行时,在文本框Text1中依次输入每件纪念品的价格(不超过w),以逗号分隔,在文本框Text2中输入限定值w,单击“分组”按钮,在标签Label2上输出最少的分组数。程序运行界面如图所示。
Private Sub Command1 Click()
Dim a(1 To 1000), n As Integer, w As Integer, cnt As Integer
Dim i As Integer, j As Integer, t As Integer
Dim s As String, ch As String
Dim p As Integer, q As Integer
s= Text1. Text: w= Val (Text2 Text): n=0
For i= 1 To Len (s)
ch= Mid(s, i, 1)
If ch=“,”Then
n=n+1:a(n)=t:t=0
Else
t=
End If
Next i
For i= n To 2 Step-1
For
j= 1 To ‘
If a(i)< a(j+1)Then
t=a(j):a(j)=a(j+1):a(+1)=t
End If
Next j
Next i
cnt=0
p=1:q=n
Do While<p
cnt=cnt+1
If Then
p=p+1: q=q-1
Else
p=p+1
End If
Loop
If p= q Then cnt=cnt+1
Labe12 Caption=Str(cnt)
End Sub
试题篮