insert #t select 'A'
union select 'B'
union select 'C'
union select 'D'
union select 'E'
select a.s+b.s from #t a,#t b where a.s<b.s
union all
select a.s+b.s+c.s from #t a,#t b,#t c where a.s<b.s and b.s<c.s
union all
select a.s+b.s+c.s+d.s from #t a,#t b,#t c,#t d
where a.s<b.s and b.s<c.s and c.s<d.s
union all
select a.s+b.s+c.s+d.s+e.s from #t a,#t b,#t c,#t d,#t e
where a.s<b.s and b.s<c.s and c.s<d.s and d.s<e.s
我的算法:
Sub getall(ParamArray x())
Dim n As Integer, i As Long, num As Integer, j As Integer
Dim temp1 As String, temp2 As Long
n = UBound(x) + 1
For num = 1 To n
For i = 0 To n ^ num - 1
temp1 = ""
temp2 = i
For j = 1 To num
temp1 = x(temp2 Mod n) & temp1
temp2 = temp2 \ n
Next
Debug.Print temp1 & vbCrLf
Next
Next
Debug.Print "共 " & (n ^ (n + 1) - 1) / (n - 1) & " 种组合!"
End Sub
Private Sub Command1_Click()
getall "A", "B", "C", "D", "E"
End Sub
Private Sub Test(intLenth As Integer)
Dim i As Integer
Dim strOut As String
For i = 31 To 0 Step -1 '二进制 00000~11111
strOut = ""
If (i And 16) > 0 Then strOut = strOut + "A"
If (i And 8) > 0 Then strOut = strOut + "B"
If (i And 4) > 0 Then strOut = strOut + "C"
If (i And 2) > 0 Then strOut = strOut + "D"
If (i And 1) > 0 Then strOut = strOut + "E"
If Len(strOut) = intLenth Or intLenth = 0 Then Debug.Print strOut
Next
End Sub
Private Sub Command1_Click()
Call Test(0) '输出所有组合
Call Test(3) '输出长度为3的组合
End Sub
Private Sub Test()
Dim i As Integer
Dim strOut As String
For i = 31 To 0 Step -1 '二进制 00000~11111
strOut = ""
If (i And 16) > 0 Then strOut = strOut + "A"
If (i And 8) > 0 Then strOut = strOut + "B"
If (i And 4) > 0 Then strOut = strOut + "C"
If (i And 2) > 0 Then strOut = strOut + "D"
If (i And 1) > 0 Then strOut = strOut + "E"
Debug.Print strOut
Next
End Sub
Private Sub Test()
Dim i As Integer
Dim strOut As String
For i = 0 To 31 '二进制 00000~11111
strOut = ""
If (i And 16) > 0 Then strOut = strOut + "A"
If (i And 8) > 0 Then strOut = strOut + "B"
If (i And 4) > 0 Then strOut = strOut + "C"
If (i And 2) > 0 Then strOut = strOut + "D"
If (i And 1) > 0 Then strOut = strOut + "E"
Debug.Print strOut
Next
End Sub