vb中怎样在一组有序一维数组中插入指定数字并使其新数组按照从小到大的顺序排列 下面是我的代码

VB在一个有序数组中插入指定数字并使其仍然有序,下面是我的代码。。12OptionBase1Dima()AsInteger,nAsInteger,iAsIntegerPr... VB 在一个有序数组中插入指定数字并使其仍然有序,下面是我的代码。。12

Option Base 1
Dim a() As Integer, n As Integer, i As Integer
Private Sub Command1_Click()

n = Val(InputBox("输入数组个数"))
ReDim a(n)
Randomize
For i = 1 To n
a(i) = Int(90 * Rnd) + 10
List1.AddItem a(i)
Next i
End Sub
Private Sub Command2_Click()
Dim t As Integer
For i = 1 To n
For j = 1 To n - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
For i = 1 To n
List2.AddItem a(i)
Next i
End Sub
Private Sub Command3_Click()
展开
 我来答
gdidajj
推荐于2016-01-20 · TA获得超过240个赞
知道小有建树答主
回答量:186
采纳率:0%
帮助的人:179万
展开全部
首先click要清除list窗口,不然会累加,问了两次,分不要浪费了!!
Option Base 1
Dim a() As Integer, n As Integer, i As Integer
Private Sub Command1_Click()
List1.Clear
List2.Clear
n = Val(InputBox("输入数组个数"))
ReDim a(1 To n)
Randomize
For i = 1 To n
a(i) = Int(90 * Rnd) + 10
List1.AddItem a(i)
Next i
End Sub
Private Sub Command2_Click()
List2.Clear
Dim t As Integer
For i = 1 To n
For j = i To n
If a(i) > a(j) Then
t = a(j)
a(j) = a(i)
a(i) = t
End If
Next j
Next i
For i = 1 To n
List2.AddItem a(i)
Next i
End Sub
Private Sub Command3_Click()
Dim i%, j%, x%
x = Val(InputBox("插入的数"))
For i = n To 1 Step -1
If x > a(i) Then Exit For
Next i
ReDim Preserve a(UBound(a) + 1)
For j = UBound(a) To i + 1 Step -1
a(j) = a(j - 1)
Next j
a(i + 1) = x
List2.Clear
For i = 1 To UBound(a)
List2.AddItem a(i)
Next i
End Sub
这样可以了吧?
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式