|
11个数120秒。
- Dim r&, m&, n&
- Sub 排数逻辑()
- Dim ar(), br(), i&
- Dim T#
- T = Timer
- r = 2
- n = [C1]: m = n * 2
- ReDim ar(0 To n)
- ReDim br(0 To m)
- For i = 0 To UBound(ar): ar(i) = i: Next i
- Call SMZ(ar, br, 0)
- [I1] = r - 2
- [L1] = Timer - T
- End Sub
- Sub SMZ(ByVal ar, ByVal br, ByVal Key)
- ' Cells(r, 1).Resize(1, m + 1) = br: Cells(r, m + 2) = Key
- ' r = r + 1
- Dim i&, StarW&
- If Key = 0 Then
- For i = 1 To UBound(ar) - 1
- ar(i) = ""
- br(0) = i: br(i + 1) = i
- Call SMZ(ar, br, -1)
- ar(i) = i
- br(0) = "": br(i + 1) = ""
- Next i
- End If
- If Key = -1 Then
- For i = br(0) + 1 To UBound(ar)
- If Len(br(m - i - 1)) = 0 Then
- ar(i) = ""
- br(m) = i: br(m - i - 1) = i
- Call SMZ(ar, br, 1)
- ar(i) = i
- br(m) = "": br(m - i - 1) = ""
- End If
- Next i
- End If
- If Key > 0 Then
- Dim Pd As Boolean
- For i = 0 To UBound(ar)
- If Len(ar(i)) <> 0 Then
- Pd = True
- For StarW = Key To m
- If Len(br(StarW)) = 0 Then Exit For
- Next
- If i = 0 Then
- ar(i) = ""
- br(StarW) = i
- Call SMZ(ar, br, StarW + 1)
- ar(i) = i
- br(StarW) = ""
- Else
- If StarW + i + 1 <= m Then
- If Len(br(StarW + i + 1)) = 0 Then
- ar(i) = ""
- br(StarW) = i: br(StarW + i + 1) = i
- Call SMZ(ar, br, StarW + 1)
- ar(i) = i
- br(StarW) = "": br(StarW + i + 1) = ""
- End If
- End If
- End If
- End If
- Next i
- If Not Pd Then Cells(r, 1).Resize(1, m + 1) = br: r = r + 1
- End If
- End Sub
复制代码 |
|