小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

Select m elements from max elements with limits

I have max  integers between 1 and max,  If I choose m of them to get  a sum sums ,Suppose I have counts ways,How can I get the counts ?

I write some codes by recursion

------------------------------------

Function counts(ByVal max As Long, ByVal m As Long, ByVal sums As Long) As Long
Dim temp As Long, i As Long, j As Long
If max >= m And m = 1 Then counts = IIf(sums <= max, 1, 0)
If max >= m And m = 2 Then
For i = 1 To max - 1
For j = i + 1 To max
If i + j = sums Then counts = counts + 1
Next
Next
End If
If max >= m And m > 2 Then counts = counts(max - 1, m, sums - m) + counts(max - 1, m - 1, sums - m)
End Function

Sub xxx()
MsgBox counts(49, 6, 101)
End Sub 

Return 58446

posted on 2006-10-27 21:22  王峰炬  阅读(132)  评论(0编辑  收藏  举报

导航