|
楼主 |
发表于 2014-12-20 21:43
|
显示全部楼层
代码更新为目前最快速度版:
借鉴了lee1982的改进,把所有Do循环改为更加高效的For循环。
并且由于使用数组b记录、检查各个位置的上限,效率更高了。- Function Combin_kagawa(m&, n&)
- If n > m Or n < 1 Then Exit Function
- Dim i&, j&, j2&, k&, l&
-
- k = WorksheetFunction.Combin(m, n)
- ReDim c&(1 To k, 1 To n): k = 0
-
- If m = n Then
- For l = 1 To n: c(1, l) = l: Next
- ElseIf n = 1 Then
- For l = 1 To m: c(l, 1) = l: Next
- Else
- ReDim a&(0 To n), b&(1 To n)
- For j = 1 To n - 1
- a(j) = j: b(j) = m - n + j
- Next
- a(n - 1) = n - 2
-
- For j = n - 1 To 1 Step -1
- i = a(j) + 1: a(j) = i
- If i = b(j) Then
- k = k + 1
- For l = 1 To n: c(k, l) = a(l): Next
- Else
- For j2 = j + 1 To n - 1: i = i + 1: a(j2) = i: Next
- j = n
- For i = i + 1 To m
- a(n) = i: k = k + 1
- For l = 1 To n: c(k, l) = a(l): Next
- Next
- End If
- Next
- End If
- Combin_kagawa = c
- End Function
复制代码 |
|