Backpack Problem description:
There are a series of objects that occupy part of the space size and have value. Given your fixed space cap, how can we find the highest value?
Public class knapsackproblem {
Static class item {
Int size;
Int val;
Public item (int s, int v ){
This. size = s;
This. Val = V;
}
}
Static item [] = new item [5];
Static int n = 5;
Static {
Item [0] = new item (3, 4 );
Item [1] = new item (4, 5 );
Item [2] = new item (7, 10 );
Item [3] = new item (8, 11 );
Item [4] = new item (9, 13 );
}
Static int knap (INT capacity ){
Int I, space, Max, T;
For (I = 0, max = 0; I <n; I ++ ){
If (Space = capacity-item [I]. Size)> = 0 ){
If (t = knap (Space) + item [I]. Val)> MAX ){
Max = T;
}
}
}
Return Max;
}
Public static void main (string [] ARGs ){
System. Out. println (knap (17 ));
}
}
This versionCodeFor unoptimized versions, later versions will be available after I have read them.