VB求数组中(20个元素)出现次数最多的元素,还有出现几次
把20个整型数据存放到一维数组a中,找出a中出现频率最高的元素值和出现的次数。例如把1,5,4,3,5,8,5,3放到a,则出现频率最高元素值是5,次数是3...
把20个整型数据存放到一维数组a中,找出a中出现频率最高的元素值和出现的次数。例如把1,5,4,3,5,8,5,3放到a,则出现频率最高元素值是5,次数是3
展开
5个回答
展开全部
Private Sub Command1_Click()
Randomize
Print "产生20个0~10之间的随机整数:"
Dim a(20) As Integer
Dim b(10) As Integer
For i = 1 To 20
a(i) = Int(Rnd * 11)
Print a(i);
If i Mod 10 = 0 Then Print
Next i
For i = 1 To 20
b(a(i)) = b(a(i)) + 1
Next i
Max = 0: maxn = -1
For i = 0 To 10
If Max < b(i) Then Max = b(i): maxn = i
Next i
Print "出现最多的是"; maxn; "出现了"; Max; "次"
End Sub
展开全部
Private Sub Command1_Click()
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 a(j) = a(i) Then
b(j) = b(j) + 1
End If
Next i
Next j
'找出最大的重复数
For j = 1 To 20
c = IIf(c >= b(j), c, b(j))
Next j
'找出最大的重复数对应的元素下标
For j = 1 To 20
If b(j) = c Then
d = j
Exit For
End If
Next
Print "重复次数最多的元素是:"; a(d)
Print "该元素重复的次数是:"; c
End Sub
有问题你找我
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 a(j) = a(i) Then
b(j) = b(j) + 1
End If
Next i
Next j
'找出最大的重复数
For j = 1 To 20
c = IIf(c >= b(j), c, b(j))
Next j
'找出最大的重复数对应的元素下标
For j = 1 To 20
If b(j) = c Then
d = j
Exit For
End If
Next
Print "重复次数最多的元素是:"; a(d)
Print "该元素重复的次数是:"; c
End Sub
有问题你找我
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Dim k As String
k= text1.text '每个数之间用,隔开
Call Test(k)
End Sub
Sub Test(str As String) ' 创建一个子程序TEST
Dim A() As String '定义一个空的数组
A = Split(str, ",") '字符中以","为分界符把数值赋予数组A
Dim B() As Integer '定义一个空数组
Dim i As Integer
For i = LBound(A) To UBound(A)
ReDim Preserve B(i) '重新定义数组B,并保留B中的植
B(i) = Int(A(i)) '把A中的字符转化为整数赋予B
Next i
For i = LBound(B) To UBound(B)
Print B(i)
Next i
'以下开始是计算出现的次数
Dim c() As String, j, l, o, q, tem() As Integer
ReDim c(UBound(B))
q = 0
For i = 0 To UBound(B)
For j = 0 To UBound(B)
If B(i) = B(j) Then '循环判断当前数出现的次数
l = l + 1 '记录它出现的次数
End If
Next j
ReDim Preserve tem(i) 'TEM用来记录已经判断过的数
tem(i) = B(i)
For q = 0 To UBound(tem) '循环判断该数是否重复判断过
If tem(q) = B(i) Then
o = o + 1
End If
Next
If o = 1 Then '如果不是重复判断就把它保存起来
c(l) = c(l) & B(i) & " "
End If
o = 0
l = 0
Next i
For i = 0 To UBound(c)
If c(i) <> "" Then
Print "出现过" & i & "次的有:" & c(i) '输出
End If
Next
End Sub
Dim k As String
k= text1.text '每个数之间用,隔开
Call Test(k)
End Sub
Sub Test(str As String) ' 创建一个子程序TEST
Dim A() As String '定义一个空的数组
A = Split(str, ",") '字符中以","为分界符把数值赋予数组A
Dim B() As Integer '定义一个空数组
Dim i As Integer
For i = LBound(A) To UBound(A)
ReDim Preserve B(i) '重新定义数组B,并保留B中的植
B(i) = Int(A(i)) '把A中的字符转化为整数赋予B
Next i
For i = LBound(B) To UBound(B)
Print B(i)
Next i
'以下开始是计算出现的次数
Dim c() As String, j, l, o, q, tem() As Integer
ReDim c(UBound(B))
q = 0
For i = 0 To UBound(B)
For j = 0 To UBound(B)
If B(i) = B(j) Then '循环判断当前数出现的次数
l = l + 1 '记录它出现的次数
End If
Next j
ReDim Preserve tem(i) 'TEM用来记录已经判断过的数
tem(i) = B(i)
For q = 0 To UBound(tem) '循环判断该数是否重复判断过
If tem(q) = B(i) Then
o = o + 1
End If
Next
If o = 1 Then '如果不是重复判断就把它保存起来
c(l) = c(l) & B(i) & " "
End If
o = 0
l = 0
Next i
For i = 0 To UBound(c)
If c(i) <> "" Then
Print "出现过" & i & "次的有:" & c(i) '输出
End If
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Dim arr As Variant
Dim ayy(2, 7)
arr = Array(1, 5, 4, 3, 5, 8, 5, 3)
For i = 0 To 7 '排序
For j = i + 1 To 7
If arr(i) < arr(j) Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next
Next
For i = 0 To 7 '输出排序后
Print arr(i) & "排序后"
Next
''''''''''主要的算法在这里'''''''''''
dis = 0
ayy(1, dis) = arr(0)
ayy(2, 0) = 1
t1 = arr(dis)
For i = 0 To 6 '把每个元素的个数统计的arr(2,7) 里
t2 = arr(i + 1)
If t1 = t2 Then
ayy(2, dis) = 1 + ayy(2, dis)
Else
dis = dis + 1
ayy(1, dis) = arr(i + 1)
ayy(2, dis) = 1
t1 = t2
End If
Next
''''''''''主要的算法在这里'''''''''''
Ma = 0
flag = 0
For i = 0 To dis '把最多的找出来吧
' Print ayy(1, i) & "有" & ayy(2, i) & "个" '(结果是否正确可使用本句验证)
If Ma < ayy(2, i) Then
flag = i
Ma = ayy(2, i)
End If
Next
Print ayy(1, flag) & "的各数最多,有" & ayy(2, flag) & "个"
End Sub
''这个小程序挺累人的,一定要加分
Dim arr As Variant
Dim ayy(2, 7)
arr = Array(1, 5, 4, 3, 5, 8, 5, 3)
For i = 0 To 7 '排序
For j = i + 1 To 7
If arr(i) < arr(j) Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next
Next
For i = 0 To 7 '输出排序后
Print arr(i) & "排序后"
Next
''''''''''主要的算法在这里'''''''''''
dis = 0
ayy(1, dis) = arr(0)
ayy(2, 0) = 1
t1 = arr(dis)
For i = 0 To 6 '把每个元素的个数统计的arr(2,7) 里
t2 = arr(i + 1)
If t1 = t2 Then
ayy(2, dis) = 1 + ayy(2, dis)
Else
dis = dis + 1
ayy(1, dis) = arr(i + 1)
ayy(2, dis) = 1
t1 = t2
End If
Next
''''''''''主要的算法在这里'''''''''''
Ma = 0
flag = 0
For i = 0 To dis '把最多的找出来吧
' Print ayy(1, i) & "有" & ayy(2, i) & "个" '(结果是否正确可使用本句验证)
If Ma < ayy(2, i) Then
flag = i
Ma = ayy(2, i)
End If
Next
Print ayy(1, flag) & "的各数最多,有" & ayy(2, flag) & "个"
End Sub
''这个小程序挺累人的,一定要加分
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dim a(20)
dim i,x,n,m
dim l(20)
for i=1 to 20
a(i)=int(rnd*9)+1
print a(i) 打印出所有数
next
for i=1 to 20
c=0
for z=1 to 20
if a(i)=a(z) then
c=c+1 c:出现次数
end if
next
l(i)=c 算出所有数出现次数
next
for i=1 to 20
if n<l(i) then
n=l(i) 把最大的次数存放在n里
end if
next
for i=1 to 20
if n=l(i) and instr(m,a(i))=0 then 如果次数相同,数值不同 一起存放在m里
m=m & a(i) & " "
end if
next
print m 数值
print n 最大次数
dim i,x,n,m
dim l(20)
for i=1 to 20
a(i)=int(rnd*9)+1
print a(i) 打印出所有数
next
for i=1 to 20
c=0
for z=1 to 20
if a(i)=a(z) then
c=c+1 c:出现次数
end if
next
l(i)=c 算出所有数出现次数
next
for i=1 to 20
if n<l(i) then
n=l(i) 把最大的次数存放在n里
end if
next
for i=1 to 20
if n=l(i) and instr(m,a(i))=0 then 如果次数相同,数值不同 一起存放在m里
m=m & a(i) & " "
end if
next
print m 数值
print n 最大次数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询