递归实现数字排列组合

参考彭希仁的递归算法

Sub macro1()
Dim arr() As String
getall 9, 3, "", arr
Debug.Print Join(arr, ",")
End Sub
Sub getall(ByVal m As Byte, ByVal n As Byte, ByRef a As String, ByRef arr() As String, Optional ByRef count As Long)
    If Len(a) = n Then
        count = count + 1
        ReDim Preserve arr(1 To count)
        arr(count) = a
        Exit Sub
    End If
    For i = 0 To m 'Val(Right(a, 1)) To m
        If InStr(a, i) = 0 Then getall m, n, a & i, arr, count
    Next i
End Sub 

你可能感兴趣的:(算法)