小李为了解决该问题,首先结合数组a的有序性,找出a,b两个数组中要剔除的m个数据,再将数组b中保留的数据插入到数组a中。示例如下:
n=10,数组a:2,4,6,8,10,12,14,16,18,20 m=5,数组b:41 , 11,17 , 29,9 处理后的数组a:2,4,6,8,9 , 10,11 , 12,14,16 |
Const n=1000,m=50
Dim a(1 To n)As Integer, b(1 To m)As Integer
Function getImax(k As Integer)As Integer
‘在数组元素b(1)~b(k)中找出值最大的元素,返回其下标,代码略
End Function
Private Sub Commandl_Click( )
Dim i As Integer, j As Integer, k As Integer
Dim na As Integer, nb As Integer, imax As Integer, mean As Integer
‘读取数据分别存入数组a、b,代码略
na=n : nb=m
imax=getImax(nb)
For i=1 To m
If a(na)<= Then
b(imax)=b(nb)
nb=nb-1
If nb>0 Then imax=getImax(nb)
Else
na=na-1
End If
Next i
Do While nb>0
i=1 : j=na
imax= getImax(nb)
Do While i<=j
mean =(i+j)\2
If a(mean)<b(imax) Then i= mean +1 Else j=mean-1
Loop
For k=na To 1 Step-1
Next k
a(i+nb-1)=b(imax)
b(imax)=b(nb)
nb=nb-1
Loop
'输出数组a,代码略
End Sub