Just explain the recursive formula
M [I] [J] = max (M [I-1] [J], M [I-1] [J-W [I] + V [I])
M [I] [J] indicates the maximum value that can be obtained when the first I object is loaded into a package with the maximum capacity of J,
If this solution does not include an I object, it is equal to M [I-1] [J], that is, the maximum value obtained by locking the first I-1 object into a bag with a capacity of J;
If I object is included, it is equal to M [I-1] [J-W [I] + V [I]. To put the object I, it must be before I, the package must have at least W [I] capacity remaining, that is, the maximum value of the current package is m [I-1] [J-W [I]. After placing object I, the maximum value is added with the value of object I. V [I];
# Include <iostream> # include <vector> using namespace STD; int max (int A, int B) {return A> B? A: B;} int main () {int W [] = {,}; // weight of each problem int V [] =, 4,6}; // The value of each question int c = 10; // The packet capacity int M [6] [11] = {0 }; // M [I] [J] indicates the maximum value of for (INT I = 1; I <6; I ++) {for (Int J = 1; j <11; j ++) {If (j> = W [I]) {M [I] [J] = max (M [I-1] [J], M [I-1] [J-W [I] + V [I]); // recursive formula }}for (INT I = 0; I <6; I ++) {for (Int J = 0; j <11; j ++) {cout <m [I] [J] <";}cout <Endl ;}cout <m [5] [10] <Endl; // put five objects into a pack with a capacity of 10 to obtain the maximum value of the lock getchar (); Return 0 ;}