这个很好弄啊! 我先用TextBox 当背包吧! 在Form1窗体里面放入一个TextBox控件,然后复制这个TextBox控件 再粘贴到窗体中 提示"已经有一个控件Text 是否创建数组?" 点击'是' 下面是代码 已经测试通过!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
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
我先用TextBox 当背包吧!
在Form1窗体里面放入一个TextBox控件,然后复制这个TextBox控件 再粘贴到窗体中 提示"已经有一个控件Text 是否创建数组?" 点击'是'
下面是代码 已经测试通过!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
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