VB 求代码啊! 把20个整型数据存放到一维数组a中,找出a中出现频率最高的元素值和出现的次数

.例如把1,5,4,3,5,8,5,3放到a,则出现频率最高元素值是5,次数是3

Private Sub Command1_Click()
Dim a(19) As Double
Dim b(19) As Double
Dim c(19) As Boolean
Dim t
Dim maxx As Double
For i = 0 To 19
t = InputBox("请输入第" & i + 1 & "个数:")
Do While Not IsNumeric(t)
t = InputBox("请输入第" & i + 1 & "个数:")
Loop
a(i) = t
Next i
For i = 0 To 19
t = a(i)
If c(i) = False Then
For j = i + 1 To 19
If t = a(j) Then b(i) = b(i) + 1: c(j) = True
Next j
End If
Next i
t = 0
For i = 0 To 19
If t < b(i) Then t = b(i): maxx = a(i)
Next i
MsgBox "出现最高的元素是:" & maxx & ",出现的次数为:" & t + 1
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-12-01
Dim a(19) As Integer
Dim 元素值(19) As Integer
Dim 元素频率(19) As Integer
Dim 元素序号 As integer = 0
Dim 新元素 As Boolean
'假设a(19)已经完成了赋值
For i = 0 to 19
新元素 = True
'搜索a(i)是否在已保存的元素值中,如果在,则频率计数+1
For k = 0 to 元素序号
if 元素值(k) = a(i) Then
元素频率(k) +=1
新元素 = False
Exit For
End If
Next
'如果搜索不到,则将新元素添加到元素值列表,频率计数+1
If 新元素 = True Then
元素值(元素序号) = a(i)
元素频率(元素序号) +=1
元素序号+=1
End If
Next
'找到最大的元素值
Dim 最大值序号 As Integer
For n = 1 to 元素序号 -1
if 元素值(n) > 元素值(n-1) Then 最大值序号 = n
Next
'返回最大值和频率
Msgbox("最高元素值为" & Str(元素值(最大值序号)) & "出现次数为" & Str(元素频率(最大值序号)))

PS:VB6.0没有集合就是麻烦。。。
你的题目没有说明当两个值出现次数一样的情况怎么办,本程序默认取最先出现的值

VB 求代码啊! 把20个整型数据存放到一维数组a中,找出a中出现频率最高的...
If t < b(i) Then t = b(i): maxx = a(i)Next i MsgBox "出现最高的元素是:" & maxx & ",出现的次数为:" & t + 1 End Sub

VB求数组中(20个元素)出现次数最多的元素,还有出现几次
Dim a(1 To 20) As Integer'存放数组a的元素 Dim b(1 To 20) As Integer'存放数组a中每个元素重复的个数 Dim c, d As Integer For i = 1 To 20'给数组a的每个元素赋值 a(i) = inputbox("a(" & i & ")的值是:") '计算每个元素重复的次数 For j = 1 To 20 For i = 1 To 20 If ...

...20个50-108(包括50和108)的整数赋给一个一维数组,求其最大值,_百度...
1、首先在桌面上,点击“Microsoft Visual Studio 2010”图标。2、然后在该界面中,输入随机产生20个50-108(包括50和108)的整数赋给一个一维数组的代码“Dim arr(1 To 20) As Integer Dim max As Integer For i = 1 To 20 arr(i) = Int(Rnd() * 58 + 50)”。4、最后在该界面中,输入...

...产生20个10至100间的随机整数存入一维数组A中,每行5个元素输出;定 ...
Private Sub Command1_Click()Dim a(20) As Integer, b(4, 5) As Integer Randomize For i = 1 To 20 a(i) = Int(Rnd * 91) + 10 Print a(i);If i Mod 5 = 0 Then Print Next Print For i = 1 To 4 For j = 1 To 5 b(i, j) = a(j + (i - 1) * 5)Print...

VB题:定义一个具有20个元素的一维数组,并对其随机赋值,然后求出其中...
Dim A(1 To 20) As Single, Sum As Single, Str As String Sum = 0: Str = ""For i = 1 To 20 Randomize A(i) = Int(100 * Rnd + 1)Str = Str & Format(CStr(A(i)), "@@@")If A(i) Mod 2 <> 0 Then Sum = Sum + A(i)Str = Str & " 奇数"End If Str ...

VB将15个整数放到一维数组中,输出该数组中的最大值它的下标。然后将它和...
Private Sub Command1_Click() Dim a(14) As Integer maxi = 0 For i = 0 To 14 a(i) = Int(Rnd * 100) If a(maxi) < a(i) Then maxi = i Print a(i); Next i Print t = a(0): a(0) = a(maxi): a(maxi) = t Print "对换以后:" For i ...

...的整数,存放在一个一维数组中,显示所有最小的元素,并统计个数_百度...
Private Sub Command1_Click() Cls Dim a(1 To 100) As Integer, min As Integer, n As Integer Randomize min = 100 Print "产生的随机数为:" For i = 1 To 100 a(i) = Int(Rnd * 100) + 1 Print Tab(5 * ((i - 1) Mod 10)); a(i); If ...

...整数,存放在一个一维数组中,显示所有小于60的元素,并统计个数 vb函...
Private Sub Command1_Click()Dim a(1 To 100) As Integer Cls For i = 1 To 100 Randomize a(i) = Int(Rnd * 100 + 1)If a(i) < 60 Then n = n + 1 Print a(i);If n Mod 10 = 0 Then Print End If Next i Print Print "小于60的元素共有"; n; "个"End Sub ...

...的随机整数存入一维数组。求出最大值并和数组中的首个
Private Sub Command1_Click()  Randomize  Dim a(9) As Integer, Max As Integer  Dim k As Integer, i As Integer  Dim t As Integer  Max = 0  For i = 0 To 9    a(i) = Int(Rnd * 51)    Print a(i);&#...

vb 怎样求数组元素个数
Option Base 1 Private Sub Form_Click()Dim i As Integer, j As Integer, k As Integer Dim count As Long Dim a(3, -2 To 2, 5) As Integer For i = LBound(a, 1) To UBound(a, 1)For j = LBound(a, 2) To UBound(a, 2)For k = LBound(a, 3) To UBound(a, 3)...

相似回答