Option ExplicitPrivate Sub Command1_Click() Dim i As Integer, j As Integer Dim s(7) As Integer
s(0) = 4 s(1) = 7 s(2) = 12 s(3) = 15 s(4) = 20 s(5) = 21 s(6) = 27 s(7) = 28 For i = 0 To 7 For j = 1 To 7 Print s(i); s(j) Next j Next i End Sub 只写了个定向的。凑合着看吧,也就帮这么多了哈~
End SubPrivate Sub test(ByVal intStart As Integer, ByVal intLev As Integer, Optional strOutput As String) Dim i As Integer DoEvents If intLev > gintGroup Then Debug.Print IIf(gCount < 10, " ", "") & gCount & ":" & strOutput: gCount = gCount + 1: Exit Sub For i = intStart + 1 To gintNum Call test(i, intLev + 1, strOutput + " " + ArrSrc(i - 1)) Next End Sub
组合和排列是二个概念,看你题意可能又是和彩票相关,应该是组合 网上有许多递归的算法,个人喜欢非递归的,自己研究的进位算法,觉得效率还可以,参考一下吧:Option Explicit'进位 Private Sub Carry(Index() As Long, M As Long, N As Long) Dim Idx As Long Dim V As Long Idx = N V = M - N Do Index(Idx) = Index(Idx) + 1 If Index(Idx) > V + Idx Then Idx = Idx - 1 Else Exit Do End If Loop Do While Idx < N Idx = Idx + 1 Index(Idx) = Index(Idx - 1) + 1 Loop End Sub'输出组合 Private Sub PrintComb(anyInput As Variant, Idx() As Long) Dim i As Long For i = 1 To UBound(Idx) Debug.Print anyInput(Idx(i) - 1); Chr(32); Next Debug.Print End Sub Private Sub GetComb(anyInput As Variant, ByVal N As Long)
Dim M As Long Dim Index() As Long Dim i As Long, j As Long
M = UBound(anyInput) + 1 ReDim Index(N) For i = 1 To N Index(i) = i Next Index(0) = -1
Do PrintComb anyInput, Index Carry Index, M, N Loop Until Index(0) = 0
End SubPrivate Sub Command1_Click()
Dim a a = Array("04", "07", "12", "15", "20", "21", "27", "28") GetComb a, 6
Option ExplicitPrivate Sub Command1_Click()
Dim i As Integer, j As Integer
Dim s(7) As Integer
s(0) = 4
s(1) = 7
s(2) = 12
s(3) = 15
s(4) = 20
s(5) = 21
s(6) = 27
s(7) = 28 For i = 0 To 7
For j = 1 To 7
Print s(i); s(j)
Next j
Next i
End Sub
只写了个定向的。凑合着看吧,也就帮这么多了哈~
blog.csdn.net/northwolves/category/27624.aspx
Private ArrSrc(), gCount As Long '用来存放源数据的数组,组合编号Private Sub Command1_Click()
Const FETCH_NUM = 6 '取几个
ArrSrc = Array("04", "07", "12", "15", "20", "21", "27", "28")
gintNum = UBound(ArrSrc) + 1
gintGroup = FETCH_NUM
gCount = 1
Call test(0, 1)
End SubPrivate Sub test(ByVal intStart As Integer, ByVal intLev As Integer, Optional strOutput As String)
Dim i As Integer
DoEvents
If intLev > gintGroup Then Debug.Print IIf(gCount < 10, " ", "") & gCount & ":" & strOutput: gCount = gCount + 1: Exit Sub
For i = intStart + 1 To gintNum
Call test(i, intLev + 1, strOutput + " " + ArrSrc(i - 1))
Next
End Sub
网上有许多递归的算法,个人喜欢非递归的,自己研究的进位算法,觉得效率还可以,参考一下吧:Option Explicit'进位
Private Sub Carry(Index() As Long, M As Long, N As Long)
Dim Idx As Long
Dim V As Long
Idx = N
V = M - N
Do
Index(Idx) = Index(Idx) + 1
If Index(Idx) > V + Idx Then
Idx = Idx - 1
Else
Exit Do
End If
Loop
Do While Idx < N
Idx = Idx + 1
Index(Idx) = Index(Idx - 1) + 1
Loop
End Sub'输出组合
Private Sub PrintComb(anyInput As Variant, Idx() As Long)
Dim i As Long
For i = 1 To UBound(Idx)
Debug.Print anyInput(Idx(i) - 1); Chr(32);
Next
Debug.Print
End Sub
Private Sub GetComb(anyInput As Variant, ByVal N As Long)
Dim M As Long
Dim Index() As Long
Dim i As Long, j As Long
M = UBound(anyInput) + 1
ReDim Index(N)
For i = 1 To N
Index(i) = i
Next
Index(0) = -1
Do
PrintComb anyInput, Index
Carry Index, M, N
Loop Until Index(0) = 0
End SubPrivate Sub Command1_Click()
Dim a a = Array("04", "07", "12", "15", "20", "21", "27", "28")
GetComb a, 6
End Sub