|
本帖最后由 doryan 于 2012-9-14 23:52 编辑
试着做做 ,先从大道小排序下 单元格 A3:B8 放的楼主的数据 解析结果74根- Option Base 1
- Sub test(Lg As Integer, Arr)
- Dim Bd: Bd = UBound(Arr, 1)
- Dim jiefa()
- Dim rst()
- Dim havejie As Boolean
- For w = Bd To 1 Step -1
- For s = Bd To 1 Step -1
- For i = s To 1 Step -1
- If asum + Arr(i, 1) <= Lg - Arr(Bd - w + 1, 1) Then
- asum = asum + Arr(i, 1)
- express = express & i & "+"
- i = i + 1
- outfor = True
- Else
- If outfor Then
- jie = jie + 1
- express = express & Bd - w + 1
- ReDim Preserve jiefa(2, jie)
- jiefa(1, jie) = asum + Arr(Bd - w + 1, 1)
- jiefa(2, jie) = express
- End If
- Exit For
- End If
- Next
- asum = 0
- express = ""
- If Not outfor Or i = 1 Then
- comb = comb + 1
- ReDim Preserve rst(comb)
- rst(comb) = jiefa
- ReDim jiefa(2, 1)
- outfor = False
- jie = 0
- Exit For
- Else
- outfor = False
- End If
- Next
- Next
- '.................
- Dim Jg()
- Dim jgct As Integer
- Dim nocan As Boolean
- For i = 1 To UBound(rst, 1)
- bestone = best(i, rst)
- Do While Arr(i, 2) <> 0
- If bestone <> "Null" Then
- fangan = Split(bestone, "+")
- Dim dic As New Dictionary
- Set dic = CreateObject("scripting.dictionary")
- For j = LBound(fangan) To UBound(fangan)
- If Not dic.exists(fangan(j)) Then
- dic.Add fangan(j), 1
- Else
- dic.Item(fangan(j)) = dic.Item(fangan(j)) + 1
- End If
- Next
- For Each ele In dic
- If dic.Item(ele) > Arr(ele, 2) Then nocan = True
- Next
- If Not nocan Then
- For Each ele In dic
- Arr(ele, 2) = Arr(ele, 2) - dic.Item(ele)
- Next
- jgct = jgct + 1
- ReDim Preserve Jg(jgct)
- Jg(jgct) = bestone
- Else
- bestone = best(i, rst)
- End If
- nocan = False
- Else
- nocan = False
- GoTo nextfangan
- End If
- Loop
- nextfangan:
- Next
- For i = 1 To UBound(Jg)
- tp = Split(Jg(i), "+")
- For j = LBound(tp) To UBound(tp)
- tp(j) = Arr(tp(j), 1)
- Next
- Jg(i) = Join(tp, "+")
- Next
- For i = 1 To Bd
- Do While Arr(i, 2) <> 0
- jgct = jgct + 1
- ReDim Preserve Jg(jgct)
- Jg(jgct) = CStr(Arr(i, 1))
- Arr(i, 2) = Arr(i, 2) - 1
- Loop
- Next
- Sheet1.[f4].Resize(UBound(Jg), 1) = Application.Transpose(Jg)
- End Sub
- Function best(i, ByRef rst)
- For e = 1 To UBound(rst(i), 2)
- If rst(i)(1, e) = 0 Then Exit For
- Next
- e = IIf(e > UBound(rst(i), 2), UBound(rst(i), 2), e - 1)
- If e = 0 Then
- best = "Null"
- Exit Function
- End If
- max = 1
- For j = 1 To e
- If rst(i)(1, j) > rst(i)(1, max) Then
- max = j
- ElseIf rst(i)(1, j) = rst(i)(1, max) Then
- If UBound(Split(rst(i)(2, max), "+")) > UBound(Split(rst(i)(2, j), "+")) Then
- max = j
- ElseIf UBound(Split(rst(i)(1, j))) = UBound(Split(rst(i)(1, max))) Then
- max = IIf(Application.Min(Split(rst(i)(1, j))) > Application.Min(Split(rst(i)(1, max))), j, max)
- End If
- End If
- Next
- tempA = rst(i)(1, max)
- rst(i)(1, max) = rst(i)(1, e)
- rst(i)(1, e) = tempA
- tempA = rst(i)(2, max)
- rst(i)(2, max) = rst(i)(2, e)
- rst(i)(2, e) = tempA
- rst(i)(1, e) = 0
- best = rst(i)(2, e)
- End Function
- Sub main()
- test 2000, Sheet1.Range("A3:B8").Value
- End Sub
复制代码
分别是
450+1500
450+1500
450+1500
450+1500
450+1500
450+1500
450+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
200+200+1500
600+600+800
600+600+800
800+800
800+800
800+800
800+800
700+700
700+700
700+700
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
1500
|
|