我自己还研究了Replace置换后Mid取序号法,以及Filter法,但效率都不高。所以也就不特别介绍了。- Sub AutoPermut_3_MidRplMacro()
- tms = Timer
- Dim k&, m&, n&
- n = 3: m = [a1].End(4).Row: If m < n Then Exit Sub
- AP = WorksheetFunction.Permut(m, n)
- ReDim jg(1 To AP, 0 To n)
- sj = [a1].Resize(m)
- Dim i1&, s1$, t1$, i2&, s2$, t2$, i3&, s3$, t3$
- l = Len("" & m) + 1: t = "," & String(l - 2, "0")
- For i1 = 1 To m
- s1 = s1 & Right(t & i1, l)
- Next
- For i1 = 0 To m - 1
- t1 = Right(t & i1 + 1, l - 1)
- s2 = Replace(s1, "," & t1, "")
- For i2 = 0 To m - 2
- t2 = Mid(s2, i2 * l + 2, l - 1)
- s3 = Replace(s2, "," & t2, "")
- For i3 = 0 To m - 3
- t3 = Mid(s3, i3 * l + 2, l - 1)
- k = k + 1
- jg(k, 0) = t1 & "," & t2 & "," & t3
- ' jg(k, 1) = t1 : jg(k, 2) = t2 : jg(k, 3) = t3
- jg(k, 1) = sj(t1, 1): jg(k, 2) = sj(t2, 1): jg(k, 3) = sj(t3, 1)
- Next i3, i2, i1
- MsgBox k & vbCr & Format(Timer - tms, "0.000s")
- End Sub
复制代码 算法原理是: 置换去掉当前已占用序号,然后Mid抽取剩余组合进行循环。- Sub AutoPermut_3_FilterMacro()
- tms = Timer
- Dim k&, m&, n&
- n = 3: m = [a1].End(4).Row: If m < n Then Exit Sub
- AP = WorksheetFunction.Permut(m, n)
- ReDim jg(1 To AP, 0 To n)
- sj = [a1].Resize(m)
- Dim i1&, t1$, i2&, t2$, i3&, t3$
- ReDim s0(m - 1)
- For i1 = 1 To m
- s0(i1 - 1) = i1
- Next
- For i1 = 0 To m - 1
- t1 = s0(i1): s1 = s0: s1(i1) = "-"
- s1 = Filter(s1, "-", False)
- For i2 = 0 To m - 2
- t2 = s1(i2): s2 = s1: s2(i2) = "-"
- s2 = Filter(s2, "-", False)
- For i3 = 0 To m - 3
- t3 = s2(i3)
- k = k + 1
- jg(k, 0) = t1 & "," & t2 & "," & t3
- ' jg(k, 1) = t1 : jg(k, 2) = t2 : jg(k, 3) = t3
- jg(k, 1) = sj(t1, 1): jg(k, 2) = sj(t2, 1): jg(k, 3) = sj(t3, 1)
- Next i3, i2, i1
- MsgBox k & vbCr & Format(Timer - tms, "0.000s")
- End Sub
复制代码 每次用Filter法去除已占用序号,然后循环。
|