我试用VBA试一试,共有113个不重复的组合可累加为11709的:
以下是求由9个数组合的程序,求其它个数的可删减其程序,我用了3 个多小时求出以上结果的。
Sub TEST()
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer, f As Integer, g As Integer, h As Integer, i As Integer, j As Integer, k As Integer
k = 0
For b = 1 To 48
For c = b + 1 To 49
For d = c + 1 To 50
For e = d + 1 To 51
For f = e + 1 To 52
If Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) > 11709 Then
Exit For
Else
Cells(1, 16) = "'" & b & c & d & e
For g = f + 1 To 53
If Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) + Cells(g, 1) > 11709 Then
Exit For
Else
For h = g + 1 To 54
If Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) + Cells(g, 1) + Cells(h, 1) > 11709 Then
Exit For
Else
For i = h + 1 To 55
If Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) + Cells(g, 1) + Cells(h, 1) + Cells(i, 1) > 11709 Then
Exit For
Else
For j = i + 1 To 56
If Cells(b, 1) + Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) + Cells(g, 1) + Cells(h, 1) + Cells(i, 1) + Cells(j, 1) > 11709 Then
Exit For
Else
If Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) + Cells(g, 1) + Cells(h, 1) + Cells(i, 1) + Cells(j, 1) = 11709 Then
' If Cells(a, 1) + Cells(b, 1) + Cells(c, 1) + Cells(d, 1) + Cells(e, 1) + Cells(f, 1) + Cells(g, 1) + Cells(h, 1) + Cells(i, 1) + Cells(j, 1) = 11709 Then
k = k + 1
' Cells(k, 3) = Cells(a, 1)
Cells(k, 4) = Cells(b, 1)
Cells(k, 5) = Cells(c, 1)
Cells(k, 6) = Cells(d, 1)
Cells(k, 7) = Cells(e, 1)
Cells(k, 8) = Cells(f, 1)
Cells(k, 9) = Cells(g, 1)
Cells(k, 10) = Cells(h, 1)
Cells(k, 11) = Cells(i, 1)
Cells(k, 12) = Cells(j, 1)
End If
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
Next
Next
Next
Next
End Sub