数组元素的插入 VB
程序要求:数组a中存放着升序排列的10个元素,现将一个新数据x插入到数组a中,使插入后的数组仍有序,且数组中各元素的值用随机函数产生,在窗体上分别输入插入前后的数组各元素...
程序要求:数组a中存放着升序排列的10个元素,现将一个新数据x插入到数组a中,使插入后的数组仍有序,且数组中各元素的值用随机函数产生,在窗体上分别输入插入前后的数组各元素值。
谢谢~~各位,这个问题紧急,大家帮帮忙吧~! 展开
谢谢~~各位,这个问题紧急,大家帮帮忙吧~! 展开
4个回答
展开全部
Private
Sub
Form_Click()
Form1.Cls
Dim
A(10)
As
Integer,
B(11)
As
Integer
Dim
I
As
Integer,
J
As
Integer,
X
As
Integer,
T
As
Boolean
Randomize
For
I
=
1
To
10
A(I)
=
Int(Rnd
*
100)
Next
Print
"随机产生的数组是:";
For
I
=
1
To
9
For
J
=
I
+
1
To
10
If
A(I)
>
A(J)
Then
A(0)
=
A(I)
A(I)
=
A(J)
A(J)
=
A(0)
End
If
Next
Print
A(I);
Next
Print
A(10)
X
=
Int(Val(InputBox("插入值
X
",
"输入",
Int(Rnd
*
100))))
Print
"插入";
X;
"后数组为:";
J
=
1
T
=
False
For
I
=
1
To
10
If
X
>
A(I)
Then
B(J)
=
A(I)
Else
If
T
Then
B(J)
=
A(I)
Else
B(J)
=
X
Print
"(";
B(J);
")";
J
=
J
+
1
B(J)
=
A(I)
T
=
True
End
If
End
If
Print
B(J);
J
=
J
+
1
Next
If
Not
T
Then
B(J)
=
X
Print
B(J);
End
If
Print
End
Sub
Private
Sub
Form_Load()
Form1.AutoRedraw
=
True
Print
"程序要求:数组a中存放着升序排列的10个元素,"
Print
"现将一个新数据x插入到数组a中,使插入后的数组仍有序,"
Print
"且数组中各元素的值用随机函数产生,"
Print
"在窗体上分别输入插入前后的数组各元素值。
"
Print
Print
"单击窗口开始......"
End
Sub
已经运行过。
Sub
Form_Click()
Form1.Cls
Dim
A(10)
As
Integer,
B(11)
As
Integer
Dim
I
As
Integer,
J
As
Integer,
X
As
Integer,
T
As
Boolean
Randomize
For
I
=
1
To
10
A(I)
=
Int(Rnd
*
100)
Next
"随机产生的数组是:";
For
I
=
1
To
9
For
J
=
I
+
1
To
10
If
A(I)
>
A(J)
Then
A(0)
=
A(I)
A(I)
=
A(J)
A(J)
=
A(0)
End
If
Next
A(I);
Next
A(10)
X
=
Int(Val(InputBox("插入值
X
",
"输入",
Int(Rnd
*
100))))
"插入";
X;
"后数组为:";
J
=
1
T
=
False
For
I
=
1
To
10
If
X
>
A(I)
Then
B(J)
=
A(I)
Else
If
T
Then
B(J)
=
A(I)
Else
B(J)
=
X
"(";
B(J);
")";
J
=
J
+
1
B(J)
=
A(I)
T
=
True
End
If
End
If
B(J);
J
=
J
+
1
Next
If
Not
T
Then
B(J)
=
X
B(J);
End
If
End
Sub
Private
Sub
Form_Load()
Form1.AutoRedraw
=
True
"程序要求:数组a中存放着升序排列的10个元素,"
"现将一个新数据x插入到数组a中,使插入后的数组仍有序,"
"且数组中各元素的值用随机函数产生,"
"在窗体上分别输入插入前后的数组各元素值。
"
"单击窗口开始......"
End
Sub
已经运行过。
展开全部
Private Sub Form_Click()
Form1.Cls
Dim A(10) As Integer, B(11) As Integer
Dim I As Integer, J As Integer, X As Integer, T As Boolean
Randomize
For I = 1 To 10
A(I) = Int(Rnd * 100)
Next
Print "随机产生的数组是:";
For I = 1 To 9
For J = I + 1 To 10
If A(I) > A(J) Then
A(0) = A(I)
A(I) = A(J)
A(J) = A(0)
End If
Next
Print A(I);
Next
Print A(10)
X = Int(Val(InputBox("插入值 X ", "输入", Int(Rnd * 100))))
Print "插入"; X; "后数组为:";
J = 1
T = False
For I = 1 To 10
If X > A(I) Then
B(J) = A(I)
Else
If T Then
B(J) = A(I)
Else
B(J) = X
Print "("; B(J); ")";
J = J + 1
B(J) = A(I)
T = True
End If
End If
Print B(J);
J = J + 1
Next
If Not T Then
B(J) = X
Print B(J);
End If
Print
End Sub
Private Sub Form_Load()
Form1.AutoRedraw = True
Print "程序要求:数组a中存放着升序排列的10个元素,"
Print "现将一个新数据x插入到数组a中,使插入后的数组仍有序,"
Print "且数组中各元素的值用随机函数产生,"
Print "在窗体上分别输入插入前后的数组各元素值。 "
Print
Print "单击窗口开始......"
End Sub
已经运行过。
Form1.Cls
Dim A(10) As Integer, B(11) As Integer
Dim I As Integer, J As Integer, X As Integer, T As Boolean
Randomize
For I = 1 To 10
A(I) = Int(Rnd * 100)
Next
Print "随机产生的数组是:";
For I = 1 To 9
For J = I + 1 To 10
If A(I) > A(J) Then
A(0) = A(I)
A(I) = A(J)
A(J) = A(0)
End If
Next
Print A(I);
Next
Print A(10)
X = Int(Val(InputBox("插入值 X ", "输入", Int(Rnd * 100))))
Print "插入"; X; "后数组为:";
J = 1
T = False
For I = 1 To 10
If X > A(I) Then
B(J) = A(I)
Else
If T Then
B(J) = A(I)
Else
B(J) = X
Print "("; B(J); ")";
J = J + 1
B(J) = A(I)
T = True
End If
End If
Print B(J);
J = J + 1
Next
If Not T Then
B(J) = X
Print B(J);
End If
End Sub
Private Sub Form_Load()
Form1.AutoRedraw = True
Print "程序要求:数组a中存放着升序排列的10个元素,"
Print "现将一个新数据x插入到数组a中,使插入后的数组仍有序,"
Print "且数组中各元素的值用随机函数产生,"
Print "在窗体上分别输入插入前后的数组各元素值。 "
Print "单击窗口开始......"
End Sub
已经运行过。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private
Sub
Form_Click()
Form1.Cls
Dim
A(10)
As
Integer,
B(11)
As
Integer
Dim
I
As
Integer,
J
As
Integer,
X
As
Integer,
T
As
Boolean
Randomize
For
I
=
1
To
10
A(I)
=
Int(Rnd
*
100)
Next
Print
"随机产生的数组是:";
For
I
=
1
To
9
For
J
=
I
+
1
To
10
If
A(I)
>
A(J)
Then
A(0)
=
A(I)
A(I)
=
A(J)
A(J)
=
A(0)
End
If
Next
Print
A(I);
Next
Print
A(10)
X
=
Int(Val(InputBox("插入值
X
",
"输入",
Int(Rnd
*
100))))
Print
"插入";
X;
"后数组为:";
J
=
1
T
=
False
For
I
=
1
To
10
If
X
>
A(I)
Then
B(J)
=
A(I)
Else
If
T
Then
B(J)
=
A(I)
Else
B(J)
=
X
Print
"(";
B(J);
")";
J
=
J
+
1
B(J)
=
A(I)
T
=
True
End
If
End
If
Print
B(J);
J
=
J
+
1
Next
If
Not
T
Then
B(J)
=
X
Print
B(J);
End
If
Print
End
Sub
Private
Sub
Form_Load()
Form1.AutoRedraw
=
True
Print
"程序要求:数组a中存放着升序排列的10个元素,"
Print
"现将一个新数据x插入到数组a中,使插入后的数组仍有序,"
Print
"且数组中各元素的值用随机函数产生,"
Print
"在窗体上分别输入插入前后的数组各元素值。
"
Print
Print
"单击窗口开始......"
End
Sub
已经运行过。
Sub
Form_Click()
Form1.Cls
Dim
A(10)
As
Integer,
B(11)
As
Integer
Dim
I
As
Integer,
J
As
Integer,
X
As
Integer,
T
As
Boolean
Randomize
For
I
=
1
To
10
A(I)
=
Int(Rnd
*
100)
Next
"随机产生的数组是:";
For
I
=
1
To
9
For
J
=
I
+
1
To
10
If
A(I)
>
A(J)
Then
A(0)
=
A(I)
A(I)
=
A(J)
A(J)
=
A(0)
End
If
Next
A(I);
Next
A(10)
X
=
Int(Val(InputBox("插入值
X
",
"输入",
Int(Rnd
*
100))))
"插入";
X;
"后数组为:";
J
=
1
T
=
False
For
I
=
1
To
10
If
X
>
A(I)
Then
B(J)
=
A(I)
Else
If
T
Then
B(J)
=
A(I)
Else
B(J)
=
X
"(";
B(J);
")";
J
=
J
+
1
B(J)
=
A(I)
T
=
True
End
If
End
If
B(J);
J
=
J
+
1
Next
If
Not
T
Then
B(J)
=
X
B(J);
End
If
End
Sub
Private
Sub
Form_Load()
Form1.AutoRedraw
=
True
"程序要求:数组a中存放着升序排列的10个元素,"
"现将一个新数据x插入到数组a中,使插入后的数组仍有序,"
"且数组中各元素的值用随机函数产生,"
"在窗体上分别输入插入前后的数组各元素值。
"
"单击窗口开始......"
End
Sub
已经运行过。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ReDim Preserve a(11)
a(11)=rnd
r=rnd
for i =1 to 10
if r>a(i) then
for k=10 downto i+1 do
a(k+1)=a(k)
a(i+1)=r
a(11)=rnd
r=rnd
for i =1 to 10
if r>a(i) then
for k=10 downto i+1 do
a(k+1)=a(k)
a(i+1)=r
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询