The problem of knapsack is a classic problem in computer science. In the simplest form, including attempting to put data items of different weights
To make the backpack reach the specified total weight. You do not need to put all the options in the backpack. For example, if you want a backpack to load 20 lbs precisely, and you have five data items you can choose to put, their weight
11, 8, 7, 6, and 5 in sequence. When the number of data items selected to be put is small, humans are very good at observing
You can solve this problem. Therefore, we can calculate the sum of 8, 7, and 5 data items and 20. If you want a computer to solve this problem, you need to give the computer more detailed instructions.
The algorithm is as follows:
1. If the sum of the selected data items matches the target weight at any time in the process, the work will be completed.
2. Select the first data item. The addition of the remaining data items must conform to the target weight of the backpack minus the weight of the first data item; this is a new target weight.
3. Try the possibility of combining each type of surplus data one by one. However, note that you do not need to try all the combinations, because you only need to stop adding data items when the top and the weight of the data are greater than the target.
4. If there is a combination, discard the-'data item and repeat the entire process from the second data item.
5. Continue from the third data item until you have tried all the combinations. At this time, you know that there is no solution.
Public class beibao {<br/> static int [] A = new int [5]; // backpack weight <br/> static int [] B = new int [5]; // result array <br/> static int flag = 0; // the next candidate item <br/> static int bound = 20; // total weight <br/> static int totle = 0; // total weight after each selection <br/>/** <br/> * backpack <br/> * @ Param I <br/> * Element coordinates <br/> * @ Param leftbound <br/> * target weight <br/> * @ Param T <br/> */<br/> Public static void inserttry (int i, int leftbound, int t) {<br/> if (I <5 & leftbound <= totle) {<br/> if (a [I] <leftbound) {// The number selected currently is smaller than the total number selected, and the number selected currently is placed in the result array. The weight of the selected number is reduced from the target, recursively returning, the weight after selection is reduced from the selected number <br/> B [t ++] = A [I]; <br/> totle = totle-A [I]; <br/> leftbound = leftbound-A [I]; <br/> I ++; <br/> inserttry (I, leftbound, t ); <br/>} else if (a [I]> leftbound) {// The number of selected items is greater than the total number of selected items and does not meet the conditions, the weight after selection is removed from the selected number. recursion <br/> totle = totle-A [I]; <br/> I ++; <br/> inserttry (I, leftbound, T); <br/>}else {// the number currently selected is equal to the sum of the selected number <br/> B [T] = A [I]; <br/> return; <br/>}< br/>} The else {// array contains no elements that meet the current condition. Remove the previous value, recursion <br/> leftbound = leftbound + B [-- T]; <br/> for (INT f = 0; F <5; F ++) {<br/> if (a [f] = B [T]) {<br/> flag = ++ F; <br/> break; <br/>}< br/> B [T] = 0; <br/> totle = 0; <br/> for (INT M = flag; m <5; m ++) {<br/> totle + = A [m]; <br/>}< br/> inserttry (flag, leftbound, t ); <br/>}< br/> return; <br/>}< br/> Public static void main (string [] ARGs) {<br/> A [0] = 11; <br/> A [1] = 8; <br/> A [2] = 6; <br/> A [3] = 7; <br/> A [4] = 5; <br/> for (INT I = 0; I <5; I ++) {<br/> B [I] = 0; <br/>}< br/> for (INT I = 0; I <5; I ++) {<br/> totle + = A [I]; <br/>}< br/> inserttry (0, 20, 0 ); <br/> for (INT I = 0; I <5; I ++) {<br/> system. out. println (B [I]); <br/>}< br/>