vba - 总和最大但小于特定值的子集

标签 vba algorithm excel knapsack-problem sub-array

我有一个具有正值的数组。 例如 array= {5,4,4,3.8,2,1.7} 我需要找到一个总和最大但小于 12 的子数组。 在这种情况下,它将是 {4,4,3.8}

另一个ex数组{7,4,3,2} 在这种情况下,最大和为 12,子集为 {7,3,2}

它的算法是什么,因为我有一个长度超过 1000 的非常大的数组。 我正在用 VBA excel 编写这个程序。

谢谢。

最佳答案

为子序列尝试这个算法

Sub aargh()
Dim a As Variant
Dim limsum As Double, highestnum As Double
Dim i As Integer, j As Integer
    a = Array(5, 4, 4, 3.8, 2, 1.7)
    limsum = 12

    highestsum = 0
    For i = 0 To UBound(a)
        s = a(i)
        j = i
        Do
            If s > highestsum Then fs = i: ls = j: highestsum = s
            j = j + 1
            If j > UBound(a) Then Exit Do
            s = s + a(j)
        Loop While s <= limsum
    Next i
    MsgBox "subarray (" & fs & "," & ls & ") sum = " & highestsum
End Sub

编辑以包含以下评论并包含子集的解决方案

Sub aargh()

    Dim sol(), csol()
    a = Array(7, 4, 3, 2)

    ReDim sol(LBound(a) To UBound(a))
    ReDim csol(LBound(a) To UBound(a))
    limsum = 13
    findsum a, sol, csol, maxsum, limsum
    ss = "array "
    For i = 1 To sol(0)
        ss = ss & sep & a(sol(i))
        sep = ","
    Next i
    MsgBox ss & " sum =" & maxsum
End Sub
Sub findsum(ByRef a, ByRef sol, ByRef csol, ByRef maxsum, ByRef limsum, Optional s = 0, Optional lvl = 1, Optional si = 0)
' recursive sub
    For i = si To UBound(a)
        s = s + a(i)
        csol(lvl) = i ' current solution contains number i
        If s <= limsum Then
            If s > maxsum Then ' we found a sum greater than current max we save it
                maxsum = s
                sol(0) = lvl
                For j = 1 To lvl
                    sol(j) = csol(j)
                Next j
            End If
            If i < UBound(a) Then ' pick another number
                findsum a, sol, csol, maxsum, limsum, s, lvl + 1, i + 1
            End If
        End If
        s = s - a(i)
    Next i
End Sub

如果数组已排序(降序),则优化代码

Sub aargh()

    Dim sol(), csol()
    a = Array(20, 15, 10, 7, 6, 5, 4, 3, 2)

    ReDim sol(LBound(a) To UBound(a))
    ReDim csol(LBound(a) To UBound(a))
    limsum = 13
    findsum a, sol, csol, maxsum, limsum, UBound(a)
    ss = "array "
    For i = 1 To sol(0)
        ss = ss & sep & a(sol(i))
        sep = ","
    Next i
    MsgBox ss & " sum =" & maxsum
End Sub
Sub findsum(ByRef a, ByRef sol, ByRef csol, ByRef maxsum, ByRef limsum, si, Optional s = 0, Optional lvl = 1)
' recursive sub
    For i = si To LBound(a) Step -1
        If s + a(i) > limsum Then Exit For
        s = s + a(i)
        csol(lvl) = i    ' current solution contains number i
        If s <= limsum Then
            If s > maxsum Then    ' we found a sum greater than current max we save it
                maxsum = s
                sol(0) = lvl
                For j = 1 To lvl
                    sol(j) = csol(j)
                Next j
            End If
            If i > LBound(a) Then    ' pick another number
                findsum a, sol, csol, maxsum, limsum, i - 1, s, lvl + 1
            End If
        End If
        s = s - a(i)
        If maxsum = limsum Then Exit For 'exit if exact match
    Next i
End Sub

关于vba - 总和最大但小于特定值的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297302/

相关文章:

algorithm - 3D 中截留雨水的最大体积

excel - 对具有拼写错误、间距差异等的相似字符串进行分组

excel - IF 在 VBA 中具有多个 OR 和 AND

ms-access - me.visible = false 而不是关闭表单

excel - 您可以以编程方式访问 Excel VBA 选择吗?

string - 用于测量两个字符串中出现的大小 >=2 的子序列数量的指标

algorithm - 递归创建嵌套结构

vba - 迭代次数过多 : syntax needed to highlight the cell row only after satifsying all criteria

excel - Powershell 脚本适用于 ISE/控制台,但不适用于任务计划程序

excel - 在多个监视器上居中用户窗体