数组a存储降序排列的m个数据,数组b中存储的是升序排列的n个数据,且两个数组中存储的数据为区间[1,20]范围内的不重复的随机整数。现将两个数组的数据合并到c数组中,使c数组的数据为左右交替上升,如下表所示:
a(1)
|
a(2)
|
a(3)
|
a(4)
|
a(5)
|
19
|
17
|
6
|
4
|
3
|
a数组
b(1)
|
b(2)
|
b(3)
|
b(4)
|
b(5)
|
b(6)
|
5
|
7
|
8
|
13
|
15
|
20
|
b数组
c(1)
|
c(2)
|
c(3)
|
c(4)
|
c(5)
|
c(6)
|
c(7)
|
c(8)
|
c(9)
|
c(10)
|
c(11)
|
3
|
5
|
7
|
13
|
17
|
20
|
19
|
15
|
8
|
6
|
4
|
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