刚写的递归代码:
' add a listbox and a commandbutton on form1
Private Sub Command1_Click()
Dim temp
temp = Split(cnm(15, 10), "*")
For i = 0 To UBound(temp)
List1.AddItem temp(i)
Next
MsgBox counts
End Sub
Function cnm(ByVal n As Integer, ByVal m As Integer) As String
Dim temp, all As String
Dim i As Long, j As Long
If m = 0 Then cnm = ""
If m = 1 Then
cnm = 1
For i = 2 To n
cnm = cnm & "*" & i
Next
End If
If m = 2 Then
For i = 1 To n - 1
For j = i + 1 To n
cnm = cnm & "*" & i & "," & j
Next
Next
End If
If m > 2 And n >= m Then
temp = Split(cnm(n - 1, m - 1), "*")
For i = 0 To UBound(temp)
If temp(i) <> "" Then temp(i) = temp(i) & "," & n
Next
cnm = Join(temp, "*")
cnm = cnm(n - 1, m) & "*" & cnm
End If
If Left(cnm, 1) = "*" Then cnm = Right(cnm, Len(cnm) - 1)
If Right(cnm, 1) = "*" Then cnm = Left(cnm, Len(cnm) - 1)
counts = UBound(Split(cnm, "*")) + 1
End Function