早教吧 育儿知识 作业答案 考试题库 百科 知识分享

VB求众数请高手用VB代码编写一个求众数的程序简单易懂谢谢!采纳有追加

题目详情
VB求众数请高手用VB代码编写一个求众数的程序 简单易懂 谢谢! 采纳有追加
▼优质解答
答案和解析
'求众数子函数 Function mode(a) Dim b&, c&(), f&(), i&, j&, k&, x() k = UBound(a) ReDim c(k), f(k) For i = 0 To k - 1 If f(i) = 0 Then c(i) = 1 For j = i + 1 To k If a(j) = a(i) Then c(i) = c(i) + 1 f(j) = 1 End If Next End If Next If f(i) = 0 Then c(i) = 1 b = 1 For i = 0 To k If c(i) > b Then b = c(i) Next '若所有数据都是众数,则没有众数 For i = 0 To k If c(i) <> b And c(i) <> 0 Then Exit For Next If i = k + 1 Then ReDim x(0) x(0) = "没有众数" mode = x Exit Function End If '找出所有众数 j = 0 For i = 0 To k If c(i) = b Then ReDim Preserve x(j) x(j) = a(i) j = j + 1 End If Next mode = x End Function '调用求众数子函数示例 Private Sub Command1_Click() Dim aa, bb aa = Array("L", 2, "k", 5, 2, 2, "L", "L") bb = mode(aa) '显示所有众数 For i = 0 To UBound(bb) Print "" & bb(i) Next End Sub
看了VB求众数请高手用VB代码编写...的网友还看了以下: