NN小于或等于的预定义数字的最佳可能组合总和

时间:2015-08-01 17:56:41

标签: javascript php excel

我有一个管道长度列表,我需要在最大允许长度内拟合这些长度以获得最佳产量

例如,最大允许长度为90,我需要制作的部分为:

25,60,13,48,23,29,27,22

为了在90岁以内最合适,我有一组这些数字:

60,29(共89个)

27,25,13,​​23(共88个)

48,22(共70个)

我发现this回答了类似的问题,但我不知道如何将其转换为在excel或javascript或php中使用

任何帮助都将不胜感激。

谢谢。

2 个答案:

答案 0 :(得分:1)

这是一种可能的解决方案。但它是一种强力算法,所以它不是那么快。

AppSettings.java

答案 1 :(得分:1)

这是基于 John Coleman的 VBA 代码。它将创建一个包含所有255个(2 8 -1)候选者的列表,并将它们按从最好到最差的顺序排列:

Sub MAIN()
    Dim i As Long, st As String
    Dim a(1 To 8) As Integer
    Dim ary

    a(1) = 25
    a(2) = 60
    a(3) = 13
    a(4) = 48
    a(5) = 23
    a(6) = 29
    a(7) = 27
    a(8) = 22

    st = ListSubsets(a)
    ary = Split(st, vbCrLf)

    For i = LBound(ary) + 1 To UBound(ary) - 1
        Cells(i, 2) = Replace(ary(i + 1), " ", "")
    Next i

    Call DistributeData
    Call SortData
End Sub

Function ListSubsets(Items As Variant) As String
    Dim CodeVector() As Integer
    Dim i As Integer
    Dim lower As Integer, upper As Integer
    Dim SubList As String
    Dim NewSub As String
    Dim done As Boolean
    Dim OddStep As Boolean

    OddStep = True
    lower = LBound(Items)
    upper = UBound(Items)

    ReDim CodeVector(lower To upper) 'it starts all 0
    Do Until done
        'Add a new subset according to current contents
        'of CodeVector

        NewSub = ""
        For i = lower To upper
            If CodeVector(i) = 1 Then
                If NewSub = "" Then
                    NewSub = Items(i)
                Else
                    NewSub = NewSub & ", " & Items(i)
                End If
            End If
        Next i
        If NewSub = "" Then NewSub = "{}" 'empty set
        SubList = SubList & vbCrLf & NewSub
        'now update code vector
        If OddStep Then
            'just flip first bit
            CodeVector(lower) = 1 - CodeVector(lower)
        Else
            'first locate first 1
            i = lower
            Do While CodeVector(i) <> 1
                i = i + 1
            Loop
            'done if i = upper:
            If i = upper Then
                done = True
            Else
                'if not done then flip the *next* bit:
                i = i + 1
                CodeVector(i) = 1 - CodeVector(i)
            End If
        End If
        OddStep = Not OddStep 'toggles between even and odd steps
    Loop
    ListSubsets = SubList
End Function

Sub DistributeData()
    Columns("B:B").Select
    Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True

        Range("A1:A255").Formula = "=if(sum(B1:I1)>=90,9999,90-sum(B1:I1))"
End Sub

Sub SortData()
    Range("A1:I255").Select
    Application.CutCopyMode = False
    ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=Range("A1:A255") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet5").Sort
        .SetRange Range("A1:I255")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

所以最好的组合是:

{60,29}和{25,13,​​29,22}

enter image description here

参考:

John Coleman's Code