Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
For a = 1 To 10
For b = 1 To 10
If b <> a Then
For c = 1 To 10
If c <> a And c <> b Then
For d = 1 To 10
If d <> a And d <> b And d <> c Then
For e = 1 To 10
If e <> a And e <> b And e <> c And e <> d Then
Debug.Print a; Space(1); b; Space(1); c; Space(1); d; Space(1); e
End If
Next
End If
Next
End If
Next
End If
Next
Next
End Sub
Private Sub Command1_Click()
pailie 15
End Sub
Sub pailie(ByVal m As Integer) 'select 5 elements from 1 to m
Dim result() As String, count As Long
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
count = 0
For a = 1 To m
For b = 1 To m
If b <> a Then
For c = 1 To m
If c <> a And c <> b Then
For d = 1 To m
If d <> a And d <> b And d <> c Then
For e = 1 To m
If e <> a And e <> b And e <> c And e <> d Then
count = count + 1
ReDim Preserve result(1 To count)
result(count) = a & Space(1) & b & Space(1) & c & Space(1) & d & Space(1) & e
End If
Next
End If
Next
End If
Next
End If
Next
Next
MsgBox "ok"
Debug.Print Join(result, vbCrLf)
Debug.Print "count=" & count
End Sub
Sub pailie(ByVal m As Integer, ByVal n As Integer)
Dim starttime As Single, endtime As Single
Dim i As Integer, j As Integer, Num As Long
Dim a() As Integer '记载数组下标
Dim b() As Integer '记载使用标识
starttime = Timer '开始计时
ReDim a(1 To m)
ReDim b(1 To m)
i = 1
a(1) = 0
Do While i <= m
a(i) = a(i) + 1
If a(i) <= m Then
If b(a(i)) = 0 Then
If i = n Then '达到数组末,则输出组合情况
For j = 1 To n
Debug.Print a(j) & " "; '输出
Next
Debug.Print
Num = Num + 1
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
DoEvents
Loop
endtime = Timer
Debug.Print "共 " & Num & " 种排列!用时 " & endtime - starttime & " 秒!"
End Sub
create table T ( s char)
insert T select '1' union select '2' union select '3' ...... union select 'a' union select 'b' ......
select a.n+b.n+c.n+d.n+e.n
from T a,T b,T c,T d,Te
where a.n<>b.n and a.n<>c.n and a.n<>d.n and a.n<>e.n
and b.n<>c.n and b.n<>d.n and b.n<>e.n
and c.n<>d.n and c.n<>e.n
and d.n<>e.n
create table T ( n int)
insert T select 1 union select 2 union select 3 ...... union select 10
select a.n*10000+b.n*1000+c.n*100+d.n*10+e.n
from T a,T b,T c,T d,Te
where a.n<>b.n and a.n<>c.n and a.n<>d.n and a.n<>e.n
and b.n<>c.n and b.n<>d.n and b.n<>e.n
and c.n<>d.n and c.n<>e.n
and d.n<>e.n