|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Private results() As Variant, resultsindex As Long
- Sub main()
- GeneratePartitions 2, 4
- Dim i As Long, j As Long, arr()
- ReDim arr(1 To UBound(results), 1 To UBound(results(1)))
- For i = 1 To UBound(arr)
- For j = 1 To UBound(arr, 2)
- arr(i, j) = results(i)(j)
- Next
- Next
- Cells(1, 1).Resize(UBound(arr), UBound(arr, 2)).Value = arr
- End Sub
- Private Function GeneratePartitions(total As Integer, numParts As Integer)
- Dim result() As Integer
- ReDim result(1 To numParts) ' 数组用来存储每种拆分的结果
- resultsindex = 0
- ReDim results(1 To 1)
- ' 调用递归函数生成拆分
- GeneratePartitionsRecursive total, numParts, 1, result
- End Function
- Private Function GeneratePartitionsRecursive(total As Integer, numParts As Integer, currentPart As Integer, result() As Integer)
- Dim i As Integer, j As Integer
-
- ' 如果当前部分等于总的部分数,则输出结果
- If currentPart = numParts Then
- result(currentPart) = total
- resultsindex = resultsindex + 1
- ReDim Preserve results(1 To resultsindex)
- results(resultsindex) = result
- Else
- ' 生成当前部分的可能性
- For i = 0 To total
- result(currentPart) = i
- ' 递归生成下一个部分
- GeneratePartitionsRecursive total - i, numParts, currentPart + 1, result
- Next i
- End If
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|