 # C++: Continuous Knapsack Problem A robber burgles a butcher’s shop, where he can select from some items. He knows the weights and prices of each items. Because he has a knapsack with 15 kg maximal capacity, he wants to select the items such that he would have his profit maximized. He may cut the items; the item has a reduced price after cutting that is proportional to the original price by the ratio of masses. That means: half of an item has half the price of the original.

This is the item list in the butcher’s:

Table of potential knapsack items
Item Weight (kg) Price (Value)
beef 3.8 36
pork 5.4 43
ham 3.6 90
greaves 2.4 45
flitch 4.0 30
brawn 2.5 56
welt 3.7 67
salami 3.0 95
sausage 5.9 98
Knapsack <=15 kg  ?

Which items does the robber carry in his knapsack so that their total weight does not exceed 15 kg, and their total value is maximised?

```#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;
double result;
double capacity = 15;
int NumberOfItems;
int number;

struct items
{
char name;
double weight;
double price;
double m;
} item;

bool cmp(items a,items b)
{
return a.price/a.weight > b.price/b.weight; // the compare function for the sorting algorithm
}

int main()
{
NumberOfItems=9;
strcpy(item.name,"beef");
item.weight=3.8;
item.price=36;

strcpy(item.name,"pork");
item.weight=5.4;
item.price=43;

strcpy(item.name,"ham");
item.weight=3.6;
item.price=90;

strcpy(item.name,"greaves");
item.weight=2.4;
item.price=45;

strcpy(item.name,"flitch");
item.weight=4.0;
item.price=30;

strcpy(item.name,"brawn");
item.weight=2.5;
item.price=56;

strcpy(item.name,"welt");
item.weight=3.7;
item.price=67;

strcpy(item.name,"salami");
item.weight=3.0;
item.price=95;

strcpy(item.name,"sausage");
item.weight=5.9;
item.price=98;

sort(item+1,item+NumberOfItems+1,cmp); // We'll sort using Introsort from STL

number = 1;
while(capacity>0&&number<=NumberOfItems)
{
if(item[number].weight<=capacity)
{
result+=item[number].price;
capacity-=item[number].weight;
item[number].m=1;
}
else
{
result+=(item[number].price)*(capacity/item[number].weight);
item[number].m=(capacity/item[number].weight);
capacity=0;

}
++number;
}

cout<<"Total Value = "<<result<<'\n';
cout<<"Total Weight = "<<(double)15-capacity<<'\n';
cout<<"Items Used:\n";
for(int i=1;i<=NumberOfItems;++i)
if(item[i].m)
{
cout<<"We took "<<item[i].m*item[i].weight<<"kg of \""<<item[i].name<<"\" and the value it brought is "<<item[i].price*item[i].m<<"\n";
}

return 0;
}
```

### Alternate Version

```// C++11 version
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct item_type
{
double  weight, value;
string  name;
};

vector< item_type > items =
{
{ 3.8, 36, "beef"    },
{ 5.4, 43, "pork"    },
{ 3.6, 90, "ham"     },
{ 2.4, 45, "greaves" },
{ 4.0, 30, "flitch"  },
{ 2.5, 56, "brawn"   },
{ 3.7, 67, "welt"    },
{ 3.0, 95, "salami"  },
{ 5.9, 98, "sausage" }
};

int main()
{
sort
(
begin( items ), end( items ),
[] (const item_type& a, const item_type& b)
{
return a.value / a.weight > b.value / b.weight;
}
);

double space = 15;

for ( const auto& item : items )
{
if ( space >= item.weight )
cout << "Take all " << item.name << endl;
else
{
cout << "Take " << space << "kg of " << item.name << endl;
break;
}

space -= item.weight;
}
}
```

SOURCE

Content is available under GNU Free Documentation License 1.2.