|
2#
楼主 |
发表于 2023-2-20 04:57:54
|
只看该作者
附上该版友原有代码(修正了一些bug)
- Public Sub ListCombinations(n As Integer, m As Integer)
- Dim a() As Variant
- ReDim a(1 To m)
- Combination 1, n, a, 1, m
- End Sub
- Private Sub Combination(t As Integer, n As Integer, a() As Variant, k As Integer, m As Integer)
- Dim i As Integer
- If k > m Then
- Debug.Print Join(a, " ")
- Exit Sub
- End If
- For i = t To n
- a(k) = i
- Combination i + 1, n, a, k + 1, m
- Next i
- End Sub
- Sub test()
- ListCombinations 5, 3
- End Sub
复制代码
相对简洁,不过仅限于数值。如需持久化(比如写入数据表或者文件),可能需要修改主函数。里面使用递归算法是代码中的点睛之笔。
|
|