|
楼主 |
发表于 2012-4-28 14:28
|
显示全部楼层
求不重复排列的更新。效率更高一些。- Sub GetPermutUnique()
- tms = Timer
- Dim AP, AC, i%, j%, k%, l%, m%, n%, p%, q%, r%, s&, t%, u%, v%, w&
- m = [a1].End(4).Row: n = [b1]
-
- ReDim a%(1 To n)
- ReDim b%(1 To n)
- ReDim c%(1 To m)
-
- trr = [a1].Resize(m)
- [a1].Resize(m).Sort [a1], 1, , , 2
- arr = [a1].Resize(m)
- [a1].Resize(m) = trr
- c(m) = m + 1
- For i = m - 1 To 1 Step -1
- If arr(i, 1) = arr(i + 1, 1) Then c(i) = c(i + 1) Else c(i) = i + 1
- Next
-
- For i = 1 To n
- a(i) = i
- b(n - i + 1) = c(m - i + 1) - 1
- Next
-
- ReDim crr(n + 1, 0)
- For i = 1 To 32767
- ' crr(i, j) = arr(a(j), 1)
- ReDim d(2, n)
- l = 0
- For j = 1 To n
- For k = 1 To n
- If d(0, k) = "" Or d(0, k) = arr(a(j), 1) Then Exit For
- Next
- If k > l Then l = k
- d(0, k) = arr(a(j), 1): d(1, k) = d(1, k) + 1
- Next
- ReDim Preserve d(2, l)
-
- For j = 1 To l
- t = d(1, j): p = j
- For k = j + 1 To l
- If d(1, k) < t Then
- t = d(1, k): p = k
- End If
- Next
- If p > j Then
- d(0, 0) = d(0, p): d(1, 0) = d(1, p)
- d(0, p) = d(0, j): d(1, p) = d(1, j)
- d(0, j) = d(0, 0): d(1, j) = d(1, 0)
- End If
- Next
-
- d(2, 1) = d(1, 1)
- s = d(1, 1)
- For j = 2 To l
- d(2, j) = d(2, j - 1) + d(1, j)
- If d(1, j) > s Then s = d(1, j)
- Next
- s = Application.Permut(n, n - s)
-
- r = 0: ReDim y(n, r)
- For j = 1 To l - 1
- For k = 1 To d(1, j)
- x = y
- ReDim y(n, s)
- t = 0
- For p = 0 To r
- For q = n - d(1, j) + k To 1 Step -1
- If x(q, p) = d(0, j) Or q = 1 Then
- For u = q To n - d(1, j) + k
- If x(u, p) = "" Then
- For v = 1 To n
- If x(v, p) <> "" Then y(v, t) = x(v, p)
- Next
- y(u, t) = d(0, j): t = t + 1
- End If
- Next
- Exit For
- End If
- Next
- Next
- r = t - 1
- ReDim Preserve y(n, r)
- Next
- Next
-
- ReDim Preserve crr(n + 1, w + r)
- For j = 0 To r
- For k = 1 To n
- ' crr(k, w + j) = y(k, j)
- crr(k, w + j) = IIf(y(k, j) = "", d(0, l), y(k, j))
- ' crr(k, w + j) = IIf(y(k, j) = "", "_", y(k, j))
- crr(0, w + j) = crr(0, w + j) & "," & crr(k, w + j)
- Next
- crr(0, w + j) = "'" & Mid(crr(0, w + j), 2)
- crr(n + 1, w + j) = i
- Next
- w = w + r + 1
-
- '__________________________
- a(n) = c(a(n))
- Chk:
- If a(n) > b(n) Then
- For j = 1 To n
- If a(j) > b(j) Then l = j - 1: Exit For
- Next j
- If l > 0 Then
- a(l) = c(a(l))
- For j = l + 1 To n
- a(j) = a(j - 1) + 1
- If a(j) > b(j) Then If a(1) > b(1) Then GoTo Ext Else GoTo Chk
- Next j
- GoTo Chk
- End If
- End If
- Next
- Ext:
- [b7] = "": [b8] = "": [d1].CurrentRegion = ""
- On Error Resume Next
- AP = WorksheetFunction.Permut(m, n)
- AC = WorksheetFunction.Combin(m, n)
- [b3] = i & "/" & AC & "/" & w & "/" & i * WorksheetFunction.Permut(n, n) & "/" & AP
- [b6] = Timer - tms
- If w + 1 > 65536 Then Exit Sub
-
- [d1].CurrentRegion = ""
- [d1].Resize(w, n + 2) = Application.Transpose(crr)
- [d1].EntireColumn.AutoFit
- [e1].Resize(1, n).EntireColumn.AutoFit
- [d1].Offset(, n + 1).ColumnWidth = 8.38
- ' [d1].CurrentRegion.Sort [d1], 1, , , 2
-
- [b7] = Timer - tms
- [b8] = [b7] - [b6]
-
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|