Multi select elements which sums upto a certain number from an array

本文介绍了一种使用VBA实现的组合求和算法,该算法能够找出数组中元素的各种组合,这些组合的总和等于给定的目标数值。文章通过具体实例展示了如何寻找一组数中能够加和到特定值的所有可能组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Give an array and a certain number,how to select some of the elements to sums upto the number?
The following codes can bring you one or more solutions out:
  1. SubSolve(ByValTotalAsDouble,ByRefData,OptionalByValfirstsolutionAsBoolean=True)'Getthefirst(orallifthe3rdparamisfalse)combintaionswhichhasasumasTotal
  2. DimFitAsBoolean,CountAsLong,Result()AsString,Used()AsByte,nAsLong,TempAsDouble'Defines
  3. n=UBound(Data)'Countofnumbers-1
  4. ReDimUsed(n)
  5. Do
  6. Fit=False'Initialize
  7. Do
  8. Fori=0Ton
  9. Used(i)=1-Used(i)'UsedorNotused.
  10. IfUsed(i)=1ThenExitFor
  11. Next
  12. Ifi>nThenExitDo'Nothingwasfound
  13. Temp=0
  14. Fori=0Ton
  15. IfUsed(i)=1ThenTemp=Temp+Data(i)'Getthesumofuseddata
  16. Next
  17. IfAbs(Temp-Total)<0.01Then'besame
  18. Fit=True'Asolutionhasbeenfound.
  19. ExitDo'Quitawhile.
  20. EndIf
  21. Loop
  22. IfFitThen'Returnthesolutionfoundjustnow.
  23. Count=Count+1'Solutioncount
  24. ReDimPreserveResult(1ToCount)'Returnthesolutionasanarray.
  25. Fori=0Ton
  26. IfUsed(i)=1ThenResult(Count)=Result(Count)&"+"&Data(i)'Theexpressionofthesolution.
  27. Next
  28. Result(Count)="Solution"&Count&":"&Total&"="&Mid(Result(Count),2)'messageofsolution
  29. Debug.PrintResult(Count)'Outputtoimmediatewindow.
  30. Iffirstsolution=TrueThenMsgBox"1solutionhasbeenfound!!!":ExitSub'Needthefirstsolutiononly.
  31. EndIf
  32. LoopWhileFit
  33. MsgBoxIIf(Count>1,Count&"solutionshave",IIf(Count=0,"No",1)&"solutionhas")&"beenfound!!!"'Threeoptionsoftheresult:0,1ormany
  34. EndSub
  35. SubGetit()'Findcombinationsofanarraywhichsumsupto1839.1
  36. Solve1839.31,Array(466.89,480.49,281.06,13.61,70.59,174.86,313.82,374.66,281.5,48.95,262.86,300.33,248.56,184.4,442.91,378.47,196.49,150.7,162.87,307.73,64.97,132.79,480.06,361.54,240.13)
  37. EndSub

It returns:

Solution1: 1839.31=13.61+70.59+174.86+313.82+248.56+442.91+378.47+196.49

If we change the codes as the following:

  1. SubGetit()'Findcombinationsofanarraywhichsumsupto1839.1
  2. Solve1839.31,Array(466.89,480.49,281.06,13.61,70.59,174.86,313.82,374.66,281.5,48.95,262.86,300.33,248.56,184.4,442.91,378.47,196.49,150.7,162.87,307.73,64.97,132.79,480.06,361.54,240.13),False
  3. EndSub

We'll get 29 solutions as the following:

Solution1: 1839.31=13.61+70.59+174.86+313.82+248.56+442.91+378.47+196.49
Solution2: 1839.31=466.89+281.06+13.61+70.59+281.5+378.47+196.49+150.7
Solution3: 1839.31=480.49+13.61+70.59+174.86+262.86+378.47+150.7+307.73
Solution4: 1839.31=281.06+13.61+70.59+174.86+313.82+281.5+48.95+196.49+150.7+307.73
Solution5: 1839.31=466.89+480.49+48.95+184.4+442.91+150.7+64.97
Solution6: 1839.31=480.49+13.61+174.86+313.82+281.5+196.49+150.7+162.87+64.97
Solution7: 1839.31=13.61+374.66+300.33+184.4+442.91+150.7+307.73+64.97
Solution8: 1839.31=13.61+313.82+48.95+300.33+248.56+378.47+162.87+307.73+64.97
Solution9: 1839.31=480.49+70.59+281.5+48.95+262.86+248.56+150.7+162.87+132.79
Solution10: 1839.31=281.06+13.61+70.59+281.5+300.33+184.4+196.49+150.7+162.87+64.97+132.79
Solution11: 1839.31=281.06+262.86+248.56+378.47+162.87+307.73+64.97+132.79
Solution12: 1839.31=466.89+70.59+174.86+48.95+262.86+184.4+150.7+480.06
Solution13: 1839.31=281.06+13.61+70.59+281.5+300.33+196.49+150.7+64.97+480.06
Solution14: 1839.31=281.06+13.61+174.86+262.86+300.33+248.56+196.49+361.54
Solution15: 1839.31=281.06+13.61+174.86+313.82+248.56+184.4+196.49+64.97+361.54
Solution16: 1839.31=70.59+174.86+281.5+48.95+378.47+150.7+307.73+64.97+361.54
Solution17: 1839.31=13.61+70.59+442.91+162.87+307.73+480.06+361.54
Solution18: 1839.31=70.59+48.95+300.33+248.56+196.49+132.79+480.06+361.54
Solution19: 1839.31=466.89+281.06+70.59+174.86+442.91+162.87+240.13
Solution20: 1839.31=374.66+281.5+248.56+184.4+196.49+150.7+162.87+240.13
Solution21: 1839.31=281.06+13.61+174.86+374.66+262.86+184.4+307.73+240.13
Solution22: 1839.31=466.89+70.59+174.86+48.95+262.86+196.49+150.7+162.87+64.97+240.13
Solution23: 1839.31=281.06+374.66+281.5+378.47+150.7+132.79+240.13
Solution24: 1839.31=70.59+374.66+48.95+184.4+307.73+132.79+480.06+240.13
Solution25: 1839.31=281.06+13.61+174.86+48.95+248.56+162.87+307.73+361.54+240.13
Solution26: 1839.31=480.49+13.61+70.59+174.86+300.33+64.97+132.79+361.54+240.13
Solution27: 1839.31=13.61+281.5+48.95+262.86+248.56+184.4+64.97+132.79+361.54+240.13
Solution28: 1839.31=313.82+262.86+300.33+162.87+64.97+132.79+361.54+240.13
Solution29: 1839.31=281.06+70.59+374.66+150.7+162.87+64.97+132.79+361.54+240.13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值