vb程序设计题,请高手解答
有两个整型数组A(N),B(M)(N>0,M>0),且已知A(1)≥A(2)≥A(3)≥…≥A(N),B(1)≥B(2)≥B(3)≥…≥B(M),请编写一段程序将数组A(...
有两个整型数组A(N),B(M)(N>0,M>0),且已知A(1)≥A(2)≥A(3)≥…≥A(N), B(1)≥B(2)≥B(3)≥…≥B(M),请编写一段程序将数组A(N)与B(M)中的元素合并为一个新的数组C(M+N),且C(1)≥C(2)≥C(3)≥…≥C(N+M)
求高手写成这道题的vb程序,并在关键步骤注释,万分感谢! 展开
求高手写成这道题的vb程序,并在关键步骤注释,万分感谢! 展开
5个回答
展开全部
Vb编程实现两个有序数组的合并,即二路归并
数组A和数组B分别记录有一些数据,这两个数组上的数据都已经由小到大按顺序排列好。请用vb编写一个程序把这此数据合并到同一个数组之中,并使得合并后的数据全部按由小到大排列。
这就是所谓的“二路归并”。在编写这个程序时先进行说明一下,首先,大量数据的输入用inputbox()函数的方法显得很烦琐,特别在调试阶段,每次运行时都要输入一大堆数据。因此采用了ARRAY函数的输入方法。
为了使大量的数据在输出时看得清楚,我们在运行窗体上设置了一个文本框,并把文本框的属性MultiLine设为True,Scorllbars设为2(带竖直滚动条)。
Vb程序代码如下:
Private Sub Form_Click()
Dim a '定义了一个可变类型的数组
a = Array(1, 3, 5, 7, 9, 11)
Dim b
b = Array(2, 4, 6, 8, 10, 12, 14, 16, 18)
Dim c(1000)
pa = 0: pb = 0: pc = 0
ea = UBound(a) '可变数组A的下标的上界和下界可以用函数UBOUND()和LBOUND()得到(若没说明下标的下界一般是0)
eb = UBound(b)
Do Until pa > ea And pb > eb
If pa > ea Then ta = b(eb) + 1 Else ta = a(pa)
If pb > eb Then tb = a(ea) + 1 Else tb = b(pb)
If ta < tb Then
c(pc) = ta
pc = pc + 1
If pa <= ea Then pa = pa + 1
Else
c(pc) = tb
pc = pc + 1
If pb <= eb Then pb = pb + 1
End If
Loop
Print ea, eb, pc
Text1.Text = "数组A的元素:" & vbCrLf
For i = 0 To ea
Text1.Text = Text1.Text & a(i) & " "
Next i
Text1.Text = Text1.Text & vbCrLf & vbCrLf & "数组A的元素:" & vbCrLf
For i = 0 To eb
Text1.Text = Text1.Text & b(i) & " "
Next i
Text1.Text = Text1.Text & vbCrLf & vbCrLf & "合并后B的元素:" & vbCrLf
For i = 0 To pc
Text1.Text = Text1.Text & c(i) & " "
Next i
End Sub
当然,你可以把这些数据全部复制到一个数组之中再使用一个排序算法来处理一次,但这样的话,原来两个数组已经分别排好的顺序就没有利用上,能否利用原来已经排好顺序的特点使得算法高效一些?
数组A和数组B分别记录有一些数据,这两个数组上的数据都已经由小到大按顺序排列好。请用vb编写一个程序把这此数据合并到同一个数组之中,并使得合并后的数据全部按由小到大排列。
这就是所谓的“二路归并”。在编写这个程序时先进行说明一下,首先,大量数据的输入用inputbox()函数的方法显得很烦琐,特别在调试阶段,每次运行时都要输入一大堆数据。因此采用了ARRAY函数的输入方法。
为了使大量的数据在输出时看得清楚,我们在运行窗体上设置了一个文本框,并把文本框的属性MultiLine设为True,Scorllbars设为2(带竖直滚动条)。
Vb程序代码如下:
Private Sub Form_Click()
Dim a '定义了一个可变类型的数组
a = Array(1, 3, 5, 7, 9, 11)
Dim b
b = Array(2, 4, 6, 8, 10, 12, 14, 16, 18)
Dim c(1000)
pa = 0: pb = 0: pc = 0
ea = UBound(a) '可变数组A的下标的上界和下界可以用函数UBOUND()和LBOUND()得到(若没说明下标的下界一般是0)
eb = UBound(b)
Do Until pa > ea And pb > eb
If pa > ea Then ta = b(eb) + 1 Else ta = a(pa)
If pb > eb Then tb = a(ea) + 1 Else tb = b(pb)
If ta < tb Then
c(pc) = ta
pc = pc + 1
If pa <= ea Then pa = pa + 1
Else
c(pc) = tb
pc = pc + 1
If pb <= eb Then pb = pb + 1
End If
Loop
Print ea, eb, pc
Text1.Text = "数组A的元素:" & vbCrLf
For i = 0 To ea
Text1.Text = Text1.Text & a(i) & " "
Next i
Text1.Text = Text1.Text & vbCrLf & vbCrLf & "数组A的元素:" & vbCrLf
For i = 0 To eb
Text1.Text = Text1.Text & b(i) & " "
Next i
Text1.Text = Text1.Text & vbCrLf & vbCrLf & "合并后B的元素:" & vbCrLf
For i = 0 To pc
Text1.Text = Text1.Text & c(i) & " "
Next i
End Sub
当然,你可以把这些数据全部复制到一个数组之中再使用一个排序算法来处理一次,但这样的话,原来两个数组已经分别排好的顺序就没有利用上,能否利用原来已经排好顺序的特点使得算法高效一些?
参考资料: http://www.sydjy.cn/bbs_list.asp?Id=2630
展开全部
注释步骤嘛,因为太累了,所以简写,估计能看个差不多
在窗体上放一个名为 text1 的TextBox控件,属性改为多行和带滚动条,再放一个按钮,按钮的响应程序如下:
Private Sub Command1_Click()
Const n = 30
Const m = 20
Dim a(n) As Integer, b(m) As Integer, c(m + n) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To n
a(i) = 3 * i
Next i
For j = 1 To m
b(j) = 4 * j
Next j
i = n
j = m
p = 0 ' a数组取净标志
q = 0 ' b
For k = 1 To m + n
If a(i) >= b(j) And p = 0 Then ' a大取a
c(k) = a(i)
If i > 0 Then
i = i - 1
Else
p = 1
End If
ElseIf b(j) > a(i) And q = 0 Then ' b大取b
c(k) = b(j)
If j > 0 Then
j = j - 1
Else
q = 1
End If
End If
If p = 1 Then ' a用完取b
c(k) = b(j)
j = j - 1
End If
If q = 1 Then ' b用完取a
c(k) = a(i)
i = i - 1
End If
Next k
' 输出至 text1
Dim ts As String
For i = 1 To m + n
ts = ts + "i=" + Str(i) + ", c(i) = " + Str(c(i)) + Chr(13) + Chr(10)
Next i
Text1.Text = ts
End Sub
运行结果如下:
i= 1, c(i) = 90
i= 2, c(i) = 87
i= 3, c(i) = 84
i= 4, c(i) = 81
i= 5, c(i) = 80
i= 6, c(i) = 78
i= 7, c(i) = 76
i= 8, c(i) = 75
i= 9, c(i) = 72
i= 10, c(i) = 72
i= 11, c(i) = 69
i= 12, c(i) = 68
i= 13, c(i) = 66
i= 14, c(i) = 64
i= 15, c(i) = 63
i= 16, c(i) = 60
i= 17, c(i) = 60
i= 18, c(i) = 57
i= 19, c(i) = 56
i= 20, c(i) = 54
i= 21, c(i) = 52
i= 22, c(i) = 51
i= 23, c(i) = 48
i= 24, c(i) = 48
i= 25, c(i) = 45
i= 26, c(i) = 44
i= 27, c(i) = 42
i= 28, c(i) = 40
i= 29, c(i) = 39
i= 30, c(i) = 36
i= 31, c(i) = 36
i= 32, c(i) = 33
i= 33, c(i) = 32
i= 34, c(i) = 30
i= 35, c(i) = 28
i= 36, c(i) = 27
i= 37, c(i) = 24
i= 38, c(i) = 24
i= 39, c(i) = 21
i= 40, c(i) = 20
i= 41, c(i) = 18
i= 42, c(i) = 16
i= 43, c(i) = 15
i= 44, c(i) = 12
i= 45, c(i) = 12
i= 46, c(i) = 9
i= 47, c(i) = 8
i= 48, c(i) = 6
i= 49, c(i) = 4
i= 50, c(i) = 3
在窗体上放一个名为 text1 的TextBox控件,属性改为多行和带滚动条,再放一个按钮,按钮的响应程序如下:
Private Sub Command1_Click()
Const n = 30
Const m = 20
Dim a(n) As Integer, b(m) As Integer, c(m + n) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To n
a(i) = 3 * i
Next i
For j = 1 To m
b(j) = 4 * j
Next j
i = n
j = m
p = 0 ' a数组取净标志
q = 0 ' b
For k = 1 To m + n
If a(i) >= b(j) And p = 0 Then ' a大取a
c(k) = a(i)
If i > 0 Then
i = i - 1
Else
p = 1
End If
ElseIf b(j) > a(i) And q = 0 Then ' b大取b
c(k) = b(j)
If j > 0 Then
j = j - 1
Else
q = 1
End If
End If
If p = 1 Then ' a用完取b
c(k) = b(j)
j = j - 1
End If
If q = 1 Then ' b用完取a
c(k) = a(i)
i = i - 1
End If
Next k
' 输出至 text1
Dim ts As String
For i = 1 To m + n
ts = ts + "i=" + Str(i) + ", c(i) = " + Str(c(i)) + Chr(13) + Chr(10)
Next i
Text1.Text = ts
End Sub
运行结果如下:
i= 1, c(i) = 90
i= 2, c(i) = 87
i= 3, c(i) = 84
i= 4, c(i) = 81
i= 5, c(i) = 80
i= 6, c(i) = 78
i= 7, c(i) = 76
i= 8, c(i) = 75
i= 9, c(i) = 72
i= 10, c(i) = 72
i= 11, c(i) = 69
i= 12, c(i) = 68
i= 13, c(i) = 66
i= 14, c(i) = 64
i= 15, c(i) = 63
i= 16, c(i) = 60
i= 17, c(i) = 60
i= 18, c(i) = 57
i= 19, c(i) = 56
i= 20, c(i) = 54
i= 21, c(i) = 52
i= 22, c(i) = 51
i= 23, c(i) = 48
i= 24, c(i) = 48
i= 25, c(i) = 45
i= 26, c(i) = 44
i= 27, c(i) = 42
i= 28, c(i) = 40
i= 29, c(i) = 39
i= 30, c(i) = 36
i= 31, c(i) = 36
i= 32, c(i) = 33
i= 33, c(i) = 32
i= 34, c(i) = 30
i= 35, c(i) = 28
i= 36, c(i) = 27
i= 37, c(i) = 24
i= 38, c(i) = 24
i= 39, c(i) = 21
i= 40, c(i) = 20
i= 41, c(i) = 18
i= 42, c(i) = 16
i= 43, c(i) = 15
i= 44, c(i) = 12
i= 45, c(i) = 12
i= 46, c(i) = 9
i= 47, c(i) = 8
i= 48, c(i) = 6
i= 49, c(i) = 4
i= 50, c(i) = 3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Dim A() As Integer, N As Integer
Dim B() As Integer, M As Integer
Dim C() As Integer, NC As Integer
Dim I As Integer, J As Integer, TM As Integer
'……前面的关于A B数组什么来头我不写了 你知道的
'定义数组C大小
NC = M + N
rdim C(NC)
'把数组A装入C
For I = 1 To N
C(I) = A(I)
Next I
'把数组B装入C
For I = 1 To M
C(N + I) = B(I)
Next I
'对C进行从大到小排序
For I = 1 To NC - 1
For J = I + 1 To NC
If C(I) < C(J) Then
TM = C(I)
C(I) = C(J)
C(J) = TM
End If
Next J
Next I
End Sub
Dim A() As Integer, N As Integer
Dim B() As Integer, M As Integer
Dim C() As Integer, NC As Integer
Dim I As Integer, J As Integer, TM As Integer
'……前面的关于A B数组什么来头我不写了 你知道的
'定义数组C大小
NC = M + N
rdim C(NC)
'把数组A装入C
For I = 1 To N
C(I) = A(I)
Next I
'把数组B装入C
For I = 1 To M
C(N + I) = B(I)
Next I
'对C进行从大到小排序
For I = 1 To NC - 1
For J = I + 1 To NC
If C(I) < C(J) Then
TM = C(I)
C(I) = C(J)
C(J) = TM
End If
Next J
Next I
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Option Explicit
Dim a(4), b(7), c(12)
Private Sub Command1_Click()
Dim i%, j%, k%, m%, n%
i = LBound(a): j = LBound(b): k = LBound(c)
m = UBound(a): n = UBound(b)
While i <= m And j <= n
If a(i) > b(j) Then
c(k) = a(i): i = i + 1: k = k + 1
Else
c(k) = b(j): j = j + 1: k = k + 1
End If
Wend
While k <= m + n - i - j + 1
If i > m Then
c(k) = b(j): j = j + 1: k = k + 1
Else
c(k) = a(i): i = i + 1: k = k + 1
End If
Wend
End Sub
Dim a(4), b(7), c(12)
Private Sub Command1_Click()
Dim i%, j%, k%, m%, n%
i = LBound(a): j = LBound(b): k = LBound(c)
m = UBound(a): n = UBound(b)
While i <= m And j <= n
If a(i) > b(j) Then
c(k) = a(i): i = i + 1: k = k + 1
Else
c(k) = b(j): j = j + 1: k = k + 1
End If
Wend
While k <= m + n - i - j + 1
If i > m Then
c(k) = b(j): j = j + 1: k = k + 1
Else
c(k) = a(i): i = i + 1: k = k + 1
End If
Wend
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
理解错误了。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询