数组a存储降序排列的m个数据,数组b中存储的是升序排列的n个数据,且两个数组中存储的数据为区间[1,20]范围内的不重复的随机整数。现将两个数组的数据合并到c数组中,使c数组的数据为左右交替上升,如下表所示:当窗体Form1加载时,自动产生a、b数组的数据,并分别显示在列表框List1与List2中,单击合并按钮Command1后,在c数组中保存按规则合并后的a、b数组的数据,并显示在列表框List3中。程序截图如下所示:
实现该功能的VB程序如下:
Const m = 5
Const n = 6
Dim a(1 To m) As Integer
Dim b(1 To n) As Integer
Dim c(1 To m + n) As Integer
‘窗体加载时,生成数组a、b中的数据,并按要求排序后显示在列表框中,代码略
Private Sub Command1_Click()
Dim pa As Integer, pb As Integer, pc As Integer, s As Integer, flag As Boolean
pa = m: pb = 1: pc = 1
flag = True
Do While ①
If a(pa) < b(pb) Then
s = a(pa)
pa = pa - 1
Else
s = b(pb)
pb = pb + 1
End If
c(pc) = s
If flag Then
pc = m + n - pc + 1
Else
pc =
End If
②
Loop
‘处理a、b数组中剩余数据,并在列表框List3中输出数组c,代码略
End Sub