|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
对于【彭版元素移动组合算法】的输出部分做了改变。
但运算部分耗时反而稍有增加。
因此,最后只是在n>m/2时有较明显的速度提升。- Sub GetCombin_Move() '原pengxi 一般算法 Reform by kagawa
- Dim i&, j&, k&, m&, n&, tms#
- tms = Timer
-
- m = [a1].End(4).Row
- n = [b1]
-
- ReDim sj1$(m, n)
- For i = 1 To m
- For j = 1 To IIf(m - i + 1 > n, n, m - i + 1)
- sj1(i, j) = sj1(i, j - 1) & Cells(i + j - 1, 1)
- Next
- Next
- ' [g1].Resize(m + 1, n + 1) = sj1
-
- ReDim a&(1 To n + 1)
- ReDim b$(1 To n + 1)
- For i = 1 To n
- a(i) = i
- Next
- a(n + 1) = m + 2 '末位加2以便能最后退出
-
- k = Application.Combin(m, n)
- ReDim jg1$(1 To k, 1 To 1)
- jg1(1, 1) = sj1(1, n)
- k = 1
- 'Open ActiveWorkbook.path & "\CombinResult.txt" For Output As #1
- Do
- For j = 1 To n
- If a(j) + 1 < a(j + 1) Then Exit For '从小到大検査当前可移位置j 即第一个可以升位的位置
- Next
- i = a(j) + 1: a(j) = i '位置作升位
- If j > 1 Then
- If a(j - 1) > j - 1 Then
- For l = 1 To j - 1
- a(l) = l
- Next
- End If
- End If
-
- t = sj1(i, 1) & b(j + 1): b(j) = t
- k = k + 1: jg1(k, 1) = sj1(1, j - 1) & t
- If j > 1 Then
- If a(j - 1) + 1 < a(j) Then
- For l = j - 1 To 1 Step -1
- i = a(l) + 1: a(l) = i '
- k = k + 1
- jg1(k, 1) = sj1(a(1), l - 1) & sj1(i, j - l) & t
- Next
- End If
- End If
- Loop Until a(1) = m - n + 1 '也可当末位超最大m停止
- 'Close #1
-
- [b14] = Format(Timer - tms, "0.000s kagawa ") & k
-
- If [c1] = "" And k < 65536 Then
- tms = Timer: [f:f] = "": [f1].Resize(k) = jg1: [f1].EntireColumn.AutoFit
- [b14] = [b14] & " " & Format(Timer - tms, "0.000s")
- End If
- Erase jg1
- End Sub
复制代码 |
|