determining which numbers in an array equal a specific sum
I am presenting this question as a general math probl开发者_Go百科em. I am going to tag it with Visual Basic, as the solution would likely be implemented in a spreadsheet.
I have a list of shipments with the number of widgets in each shipment. Each shipment was loaded on either Truck #1 or Truck #2. Given the total number of widgets on each truck, how can I determine which shipments were on each truck?
For example, here is the loading for total widgets in each truck:
Truck #1 83,240
Truck #2 63,460
-------
146,700
And here is a detailed shipment list.
SHIPMENT ID QUANTITY
90006 340
93806 2,460
93906 22,980
92506 5,960
96306 3,580
96406 3,320
96906 2,680
97306 1,160
99206 9,780
95005 15,300
95006 2,980
96008 22,320
95606 28,580
90206 5,020
90306 3,160
94006 1,140
94406 4,640
94606 7,900
98606 3,400
-------
146,700
Which shipments were in Truck #1 and Truck #2?
This is a variation of the subset sum problem. More information can be found on Stack Exchange's Math site. This problem is not trivial. One algorithm is described at the Wikipedia page. In your case, an exhaustive search is probably appropriate.
精彩评论