请教vb问题
展开全部
涉及的知识:
1,产生随机数。
2,数组。
3,排序算法。
解决思路:
1,产生随机数:
x = Int(Rnd*10)
2,本题用二维数组,第1维存放0-9,第2维存放对应出现的次数。
'定义数组,并初始化,使得数组的第一维,存放0-9的数字,即:
d(1,0)=0;d(1,1)=1;d(1,2)=2;......d(1,9)=9;
Dim d(1 to 2,0 To 9) as integer
dim i as integer
for i=0 to 9
d(1,i)=i
d(2,i)=0 '第2维设置0
next i
3,排序算法,网上很多搜一下就有。
完整代码:
'1,初始化
Dim d(1 To 2, 0 To 9) As Integer
Dim i As Integer
For i = 0 To 9
d(1, i) = i
d(2, i) = 0
Next i
'2,产生随机数,并计数
Dim x As Integer
Randomize
For i = 1 To 200
x = Int(Rnd * 10)
d(2, x) = d(2, x) + 1 '说明:假设x=5,那么d(1,5)里存放的是5,这样使得d(2,5)增加1
Next i
'3,排序
Dim j As Integer
For i = 0 To 9 - 1
For j = i + 1 To 9
If d(2, i) < d(2, j) Then
x = d(1, i)
d(1, i) = d(1, j)
d(1, j) = x
x = d(2, i)
d(2, i) = d(2, j)
d(2, j) = x
End If
Next j
Next i
'4,输出排序结果
Me.cls
Print "数字", "结果"
For i = 0 To 9
Print d(1, i), d(2, i)
Next i
1,产生随机数。
2,数组。
3,排序算法。
解决思路:
1,产生随机数:
x = Int(Rnd*10)
2,本题用二维数组,第1维存放0-9,第2维存放对应出现的次数。
'定义数组,并初始化,使得数组的第一维,存放0-9的数字,即:
d(1,0)=0;d(1,1)=1;d(1,2)=2;......d(1,9)=9;
Dim d(1 to 2,0 To 9) as integer
dim i as integer
for i=0 to 9
d(1,i)=i
d(2,i)=0 '第2维设置0
next i
3,排序算法,网上很多搜一下就有。
完整代码:
'1,初始化
Dim d(1 To 2, 0 To 9) As Integer
Dim i As Integer
For i = 0 To 9
d(1, i) = i
d(2, i) = 0
Next i
'2,产生随机数,并计数
Dim x As Integer
Randomize
For i = 1 To 200
x = Int(Rnd * 10)
d(2, x) = d(2, x) + 1 '说明:假设x=5,那么d(1,5)里存放的是5,这样使得d(2,5)增加1
Next i
'3,排序
Dim j As Integer
For i = 0 To 9 - 1
For j = i + 1 To 9
If d(2, i) < d(2, j) Then
x = d(1, i)
d(1, i) = d(1, j)
d(1, j) = x
x = d(2, i)
d(2, i) = d(2, j)
d(2, j) = x
End If
Next j
Next i
'4,输出排序结果
Me.cls
Print "数字", "结果"
For i = 0 To 9
Print d(1, i), d(2, i)
Next i
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询