2,506
社区成员
发帖
与我相关
我的任务
分享Private Sub Command1_Click()
Dim i As Integer, j As Integer, n As Integer
For i = 100 To 1 Step -1
List1.AddItem i
Next i
For i = 0 To 25 '已知需 26 个列表,可根据应用需要调整
n = 200
Do
For j = 0 To List1.ListCount - 1
If List1.List(j) <= n Then
Debug.Print List1.List(j),
n = n - List1.List(j)
List1.RemoveItem j
Exit For
End If
Next j
If List1.ListCount = 0 Then Exit Do
Loop Until n < List1.List(List1.ListCount - 1)
Debug.Print "n = " & n
Next i
End Sub
结果
100 99 1 n = 0
98 97 5 n = 0
96 95 9 n = 0
94 93 13 n = 0
92 91 17 n = 0
90 89 21 n = 0
88 87 25 n = 0
86 85 29 n = 0
84 83 33 n = 0
82 81 37 n = 0
80 79 41 n = 0
78 77 45 n = 0
76 75 49 n = 0
74 73 53 n = 0
72 71 57 n = 0
70 69 61 n = 0
68 67 65 n = 0
66 64 63 7 n = 0
62 60 59 19 n = 0
58 56 55 31 n = 0
54 52 51 43 n = 0
50 48 47 46 8 n = 1
44 42 40 39 35 n = 0
38 36 34 32 30 28 2 n = 0
27 26 24 23 22 20 18 16 15 6 3 n = 0
14 12 11 10 4 n = 149
Private Sub Form_Load()
'----------------
'这里声明你的数组名字为tmpL 为了演示简单 他的长度我设置成11个
Dim tmpNum As Long
Dim tmpL(10) As Long
tmpNum = 200 '设置总和不超过200
tmpL(10) = 199 '最大的数不能超过总和
tmpL(9) = 177
tmpL(8) = 163
tmpL(7) = 159
tmpL(6) = 155
tmpL(5) = 151
tmpL(4) = 101
tmpL(3) = 77
tmpL(2) = 55
tmpL(1) = 28
tmpL(0) = 1
'-----------------
Dim i As Integer, q As Integer
Dim tmpB() As Long
ReDim tmpB(UBound(tmpL)) As Long
Dim tmpQ As Integer, tmpT As Integer
Text1(0).Text = ""
Text1(1).Text = ""
tmpT = 1
For i = UBound(tmpL) To 0 Step -1
For tmpQ = 0 To UBound(tmpL)
If tmpL(i) + tmpB(tmpQ) <= tmpNum Then
tmpB(tmpQ) = tmpB(tmpQ) + tmpL(i)
If tmpQ > tmpT Then
tmpT = tmpT + 1
Load Text1(tmpT)
Text1(tmpT).Text = tmpL(i)
Exit For
Else
Text1(tmpQ).Text = Text1(tmpQ).Text & "," & tmpL(i)
Exit For
End If
End If
Next
Next
For i = 0 To tmpT
With Text1(i)
.Visible = True
.Top = i * 300
.Left = 10
.Height = 200
.Width = 6000
End With
Next
For i = 0 To 1
Text1(i) = Mid(Text1(i), 2, Len(Text1(i)) - 1)
Next
End Sub