在一个有序数列中插入一个n,使插入后的数列依然有序,并输出
1个回答
展开全部
Dim a() As Integer
Private Sub Command1_Click()
Cls
Randomize (Timer)
Dim n As Integer
n = InputBox("请输入所生成的数组中包含的元素个数", , 10)
ReDim a(n - 1)
For i = LBound(a) To UBound(a)
a(i) = Int(Rnd * 900 + 100)
Next i
For i = LBound(a) To UBound(a) - 1
For j = i + 1 To UBound(a)
If a(j) < a(i) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
For i = LBound(a) To UBound(a)
Print a(i);
If (i + 1) Mod 10 = 0 Then Print
Next i
End Sub
Private Sub Command2_Click()
Dim n As Single, Flag As Boolean
n = InputBox("请输入要插入的数n:", , 500)
ReDim Preserve a(UBound(a) + 1)
Flag = True
For i = LBound(a) To UBound(a)
If n < a(i) Then
For j = UBound(a) To i + 1 Step -1
a(j) = a(j - 1)
Next j
a(i) = n
Flag = False
Exit For
End If
Next i
If Flag Then a(UBound(a)) = n
Print "插入" & n & ":"
For i = LBound(a) To UBound(a)
Print a(i);
If (i + 1) Mod 10 = 0 Then Print
Next i
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询