发布网友 发布时间: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