|
- Sub Main()
- tt = Timer
- ar = Range("a1:d8").Value
- br = Range("f2:h5").Value
- n = Range("i2").Value
- ReDim cr(1 To 4)
- For i = 1 To 4
- cr(i) = GetNumofColumn(ar, i)
- Next
- ar = cr
- ReDim result(1 To 50000, 1 To n)
-
- For i1 = br(1, 2) To br(1, 3)
- For i2 = br(2, 2) To br(2, 3)
- For i3 = br(3, 2) To br(3, 3)
- For i4 = br(4, 2) To br(4, 3)
- If i1 + i2 + i3 + i4 = n Then
- j = 0
- If i1 > 0 Then j = j + 1: cr(j) = CombinM_N(ar(1), i1)
- If i2 > 0 Then j = j + 1: cr(j) = CombinM_N(ar(2), i2)
- If i3 > 0 Then j = j + 1: cr(j) = CombinM_N(ar(3), i3)
- If i4 > 0 Then j = j + 1: cr(j) = CombinM_N(ar(4), i4)
- If j < 4 Then
- ReDim temp(1, 1)
- For i = j + 1 To 4
- cr(i) = temp
- Next
- End If
- For j1 = 1 To UBound(cr(1))
- For j2 = 1 To UBound(cr(2))
- For j3 = 1 To UBound(cr(3))
- For j4 = 1 To UBound(cr(4))
- k = k + 1
- For k1 = 1 To UBound(cr(1), 2)
- If k1 <= n Then result(k, k1) = cr(1)(j1, k1)
- Next
- For k2 = 1 To UBound(cr(2), 2)
- If k1 - 1 + k2 <= n Then result(k, k1 - 1 + k2) = cr(2)(j2, k2)
- Next
- For k3 = 1 To UBound(cr(3), 2)
- If k1 + k2 - 2 + k3 <= n Then result(k, k1 + k2 - 2 + k3) = cr(3)(j3, k3)
- Next
- For k4 = 1 To UBound(cr(4), 2)
- If k1 + k2 + k3 - 3 + k4 <= n Then result(k, k1 + k2 + k3 - 3 + k4) = cr(4)(j4, k4)
- Next
- For k1 = 1 To n - 1
- For k2 = k1 + 1 To n
- If result(k, k1) > result(k, k2) Then
- k3 = result(k, k1)
- result(k, k1) = result(k, k2)
- result(k, k2) = k3
- End If
- Next
- Next
-
- Next
- Next
- Next
- Next
- End If
- Next
- Next
- Next
- Next
- Range("k1").Resize(k, n).Value = result
- MsgBox "组合数量: " & k & Chr(10) & "用时: " & Timer - tt
- End Sub
复制代码 |
|