题目:
整数数列 {xn} ∈[0,max] , 且 ∑xi=sum, 则这样的序列共有多少个?
方法:递归
Function resultcount(ByVal n As Integer, ByVal max As Integer, ByVal sum As Integer) As Integer
If n * max < sum Then resultcount = 0: Exit Function
If n = 1 Then resultcount = 1
If sum = 1 Then resultcount = n
If n > 1 Then
Dim i As Integer, temp As Integer
temp = 0
For i = 0 To max
temp = temp + resultcount(n - 1, max, sum - i)
Next
resultcount = temp
End If
End Function
博客围绕整数数列 {xn} 展开,该数列元素取值范围在 [0,max] 且元素和为 sum。提出用递归方法解决求这样的序列个数的问题,并给出了名为 resultcount 的递归函数代码实现。
883

被折叠的 条评论
为什么被折叠?



