Dim I As Integer '用作数组下标
Dim J As Integer '用作循环输出
Dim n As Integer '记载数组个数
Dim a() As Integer '记载数组下标
Dim b() As Integer '记载使用标识
n = 4 '假使数组共有四个元素
ReDim a(n)
ReDim b(n)
I = 1 '假使数组下标从1开始
a(1) = 0
Do While I <= n
a(I) = a(I) + 1
If a(I) <= n Then
If b(a(I)) = 0 Then
If I = n Then '达到数组末,则输出组合情况
'这里只输出数组下标,可使用Array(s(J))的方式输出数组内容
For J = 1 To n
Text1.SelText = a(J) & " "
Next
Text1.SelText = vbCrLf
I = I - 1
b(a(I)) = 0 '清空使用标识
Else
b(a(I)) = 1 '标记已使用
I = I + 1
a(I) = 0 '重查
End If
End If
Else
I = I - 1
If I = 0 Then Exit Do '回溯至数组前则结束
b(a(I)) = 0
End If
Loop
满意吗?
Private Sub Command1_Click()
Dim a(4) As String, temp As String
a(0) = 9
a(1) = 12
a(2) = 48
a(3) = 11
a(4) = 29
temp = permutation(a, UBound(a))
'temp = Replace(temp, ",", "")
Debug.Print temp
Debug.Print "共有 " & UBound(Split(temp, vbCrLf)) + 1 & " 种排法!"
End Sub
Function addxtostr(ByVal x0 As String, ByVal xadd As String) As String
Dim temp, temp2, all() As String, i As Long
temp = Split(x0, ",")
ReDim all(UBound(temp) + 1)
all(0) = xadd & "," & x0
For i = 1 To UBound(all)
temp2 = temp
temp2(i - 1) = temp2(i - 1) & "," & xadd
all(i) = Join(temp2, ",")
Next
addxtostr = Join(all, vbCrLf)
Set temp = Nothing
Set temp2 = Nothing
Erase all
End Function
Function permutation(ByRef a() As String, ByVal n As Long) As String
Dim i As Long, temp, all() As String
If n = 1 Then permutation = a(0)
If n = 2 Then permutation = a(0) & "," & a(1) & vbCrLf & a(1) & "," & a(0)
If n > 2 Then
temp = Split(permutation(a, n - 1), vbCrLf)
ReDim all(UBound(temp))
For i = 0 To UBound(temp)
all(i) = addxtostr(temp(i), a(n - 1))
Next
permutation = Join(all, vbCrLf)
End If
Erase all
End Function
Private Sub Command1_Click()
Dim a(8) As String, temp As String
For i = 0 To 8
a(i) = i
Next
temp = permutation(a, UBound(a))
Debug.Print temp
Debug.Print "共有 " & UBound(Split(temp, vbCrLf)) + 1 & " 种排法!"
End Sub
Function addxtostr(ByVal x0 As String, ByVal xadd As String) As String
Dim temp, temp2, all() As String, i As Long
temp = Split(x0, ",")
ReDim all(UBound(temp) + 1)
all(0) = xadd & "," & x0
For i = 1 To UBound(all)
temp2 = temp
temp2(i - 1) = temp2(i - 1) & "," & xadd
all(i) = Join(temp2, ",")
Next
addxtostr = Join(all, vbCrLf)
Set temp = Nothing
Set temp2 = Nothing
Erase all
End Function
Function permutation(ByRef a() As String, ByVal n As Long) As String
Dim i As Long, temp, temp1, temp2, temp3, all() As String
If n = 0 Then permutation = a(0)
If n = 1 Then permutation = a(0) & "," & a(1) & vbCrLf & a(1) & "," & a(0)
If n > 1 Then
temp = Split(permutation(a, n - 1), vbCrLf)
ReDim all(UBound(temp))
For i = 0 To UBound(temp)
all(i) = addxtostr(temp(i), a(n - 1))
Next
permutation = Join(all, vbCrLf)
End If
Erase all
End Function
Private Sub Command1_Click()
Const Total As Long = 4
Dim A(Total) As Long '各位取值
Dim Flag(Total) As Long '表示取值次数
Dim i As Long, j As Long, k As Long, l As Long
Dim S As Long, Count As Long
Dim Exist As Boolean
For i = 1 To Total
Flag(i) = 1
A(i) = i
Next i
S = 1
For i = 2 To Total
S = S * i
Next i
For i = 1 To Total
Text1.Text = Text1.Text & "-" & A(i)
Next i
Text1.Text = Text1.Text & vbCrLf
Count = 1
Do
For i = Total To 1 Step -1
If Flag(i) < Total - i + 1 Then
Exist = False
For j = A(i) + 1 To Total
For k = 1 To i - 1
If A(k) = j Then
Exist = True
Exit For
End If
Next k
If Not Exist Then
A(i) = j
Exit For
End If
Next j
Flag(i) = Flag(i) + 1
For l = i + 1 To Total
For j = 1 To Total
Exist = False
For k = 1 To l - 1
If A(k) = j Then
Exist = True
Exit For
End If
Next k
If Not Exist Then
A(l) = j
Exit For
End If
Next j
Flag(l) = 1
Next l
Exit For
End If
Next i
For i = 1 To Total
Text1.Text = Text1.Text & "-" & A(i)
Next i
Text1.Text = Text1.Text & vbCrLf