首页 热点资讯 义务教育 高等教育 出国留学 考研考公

VB排序,分别去掉最大最小值,然后其余数求平均值

发布网友 发布时间:2025-01-04 23:32

我来回答

1个回答

热心网友 时间:2025-01-04 23:30

'添加如下两个过程,然后调用 GetAverage 即可。
'示例: (假设要对长整型数组Arr排序并求去头尾后的平均值)
'MsgBox GetAverage(Arr)

Private Function GetAverage(Source() As Long)
InsertSort Source
Dim i As Long, Sum As Long
For i = LBound(Source) + 1 To UBound(Source) - 1
Sum = Sum + Source(i)
Next
If UBound(Source) - LBound(Source) - 2 > 0 Then GetAverage = Sum / (UBound(Source) - LBound(Source) - 2)
End Function

Private Sub InsertSort(Source() As Long)
Dim i As Long, j As Long, k As Long, c As Long
ReDim n(LBound(Source) To UBound(Source)) As Long
c = LBound(Source)
For i = LBound(Source) To UBound(Source)
For j = LBound(Source) To c - 1
If Source(i) <= n(j) Then Exit For
Next
For k = c - 1 To j Step -1
n(k + 1) = n(k)
Next
n(j) = Source(i)
c = c + 1
Next
For i = LBound(Source) To UBound(Source)
Source(i) = n(i)
Next
End Sub

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com