算法说明:从a数组中读入一枚导弹的高度数据a(i),在b数组中查找第一个大于a(i)数值的位置,并用a(i)代替。如果a(i)大于等于b数组中所有的数值,则增加一台导弹拦截车(k=k+1)。
请正在做卷子优秀的你回答下列问题。
Dim a(0 To 100) As Integer ‘记录所有导弹的高度及顺序
Dim b(0 To 100) As Integer ‘b(k)数组存储第k个系统车能拦截的最高高度
Dim n As Integer
Private Sub Command1_ Click( )
Dim L As Integer, R As Integer, m As Integer
Dim k As Integer, res As Integer, i As Integer
Randomize
n=15
For i= 1To n
a(i)= ‘随机产生高度[75,380]的导弹,高度为整数
Next i
‘将导弹高度依顺序显示在文本框Text1中,代码略
k=1:b(k)=a(1)
For i=2 To n
b(k+1)=a(i)
L= 1
R=k+1
Do While L< = R
m=(L+R+1)\2
If Then
R=m-1
res=m
Else
L=m+1
End If
Loop
If res =k+1 Then k=k+1
Next i
Label2. Caption =“最少需要”+ Str(k) +“台导弹拦截系统”
End Sub