or using dynamic programming (planning)
Problem Description:
--------------------------------------------------------------------------------------------------------------- ----------
Knapsack problem
There are n types of items, each with a weight and a value. but the number of each item is unlimited , at the same time there is a backpack, the maximum load weight of k, today from n items selected several pieces (the same item can be selected multiple times), so that its weight and less than equal to K, and the value of the maximum.
Input data:
The first row two number: The total number of items n, the backpack load weight k; two numbers are separated by a space;
The second row n number, is n kind of item weight; two numbers are separated by a space;
The number of the third row n is the value of n items; Two numbers are separated by a space.
Output data:
The total value of the first line.
Input Sample:
4 10
2 3 4 7
1 3 5 9
Sample output:
12
--------------------------------------------------------------------------------------------------------------- ----------
Law One:
1#include"iostream"2#include"Cstdio"3 #defineMAXN 81020504 using namespacestd;5 intN,W[MAXN],V[MAXN];6 intK;7 intF[MAXN];8 intMain ()9 Ten { OneCin>>n>>K; A for(intI=1; i<=k;i++) -f[i]=-2100000; - for(intI=1; i<=n;i++) theCin>>W[i]; - for(intI=1; i<=n;i++) -Cin>>V[i]; -f[0]=0; + for(intI=1; i<=n;i++) - for(intwei=w[i];wei<=k;wei++) +F[wei]= (F[wei-w[i]]+v[i]>f[wei])? f[wei-w[i]]+V[i]:f[wei]; Acout<<F[k]; at return 0; -}
Law II:
Cond....
About the complete knapsack problem