笔者曾写过利用递归实现不重复序列全排列的例子http://blog.csdn.net/northwolves/archive/2004/04/07/19590.aspx
现在给出另一个利用进制实现的代码:
Sub pailie1(ParamArray x())
Dim starttime As Single, endtime As Single
Dim n As Integer, i As Long, Num As Integer, j As Integer
Dim temp1() As String, temp2 As Long, all As New Collection
n = UBound(x) + 1 '元素个数
starttime = Timer '开始计时
ReDim temp1(1 To n)
For i = 0 To n ^ n - 1
temp2 = i
Set all = Nothing
On Error Resume Next
For j = n To 1 Step -1
temp1(j) = x(temp2 Mod n) '转换为n进制
all.Add temp1(j), temp1(j) '防止某一元素重复列出
temp2 = temp2 / n
Next
If all.Count = n Then
Debug.Print Join(temp1, " ") '输出
Num = Num + 1
End If
? DoEvents
Next
endtime = Timer
Debug.Print "共 " & Num & " 种排列!用时 " & endtime - starttime & " 秒!"
End Sub
Private Sub Command1_Click()
pailie1 "a", "b", "c", "d", "e", "f", "g"
End Sub