'男的有2个低分结果不太理想,女的可以接受
Option Explicit
Sub test()
Dim arr, brr, crr, n, ave
arr = [a1].CurrentRegion
brr = arr: Call seldata(brr, "男", n, ave)
Call try(brr, crr, n, ave): [f2].Resize(n, UBound(crr, 2)) = crr
brr = arr: Call seldata(brr, "女", n, ave)
Call try(brr, crr, n, ave): [l2].Resize(n, UBound(crr, 2)) = crr
End Sub
Function try(brr, crr, n, ave)
Dim i, j, k, sum, result, t
For i = 1 To 1000
Call rnddata(brr, n)
If i = 1 Then
For j = 1 To n - n Mod 8 Step 8
sum = 0
For k = j To j + 7: sum = sum + brr(k, 3): Next
If j = 1 Then
result = Abs(ave - sum / 8): crr = brr
Else
If result < Abs(ave - sum / 8) Then result = Abs(ave - sum / 8)
End If
Next
Else
For j = 1 To n - n Mod 8 Step 8
sum = 0
For k = j To j + 7: sum = sum + brr(k, 3): Next
If j = 1 Then
t = Abs(ave - sum / 8)
Else
If t < Abs(ave - sum / 8) Then t = Abs(ave - sum / 8)
End If
Next
If t < result Then result = t: crr = brr
End If
Next
End Function
Function seldata(arr, flag, n, ave)
Dim i, j
n = 0: ave = 0
For i = 2 To UBound(arr, 1)
If arr(i, 2) = flag Then
n = n + 1: ave = ave + arr(i, 3)
For j = 1 To UBound(arr, 2)
arr(n, j) = arr(i, j)
Next
End If
Next
ave = ave / n
End Function
Function rnddata(arr, n)
Dim i, j, t, m
Randomize
For i = 1 To n
m = Int(Rnd * n) + 1
For j = 1 To UBound(arr, 2)
t = arr(i, j): arr(i, j) = arr(m, j): arr(m, j) = t
Next j, i
End Function |