-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractionalKnapsack.cpp
More file actions
75 lines (65 loc) · 1.75 KB
/
FractionalKnapsack.cpp
File metadata and controls
75 lines (65 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<stdlib.h>
using namespace std;
int knapsack(int a[2][100],int n,int w)
{ int i,c, used[100], max = -1, profit = 0;
for (i = 0; i < n; i++)
{
used[i] = 0;
}
c = w;
while (c >= 0)
{
max = -1;
for (i = 0; i < n; i++)
{
if ((used[i] == 0) && ((max == -1) || (((float) a[1][i]
/ (float) a[0][i]) > ((float) a[1][max]
/ (float) a[0][max]))))
{
max = i;
}
}
used[max] = 1;
c -= a[0][max];
profit += a[1][max];
if (c >= 0)
{
cout << "\n\nAdded item " << max + 1 << "\n Weight: "
<< a[0][max] << "\t Profit: " << a[1][max]
<< "\n Space left: " << c;
}
else
{
cout << "\n\nAdded item " << max + 1 << "\n Weight: "
<< (a[0][max] + c) << "\t Profit: "
<< (a[1][max] / a[0][max]) * (a[0][max]
+ c) << " \n Space left: 0";
profit -= a[1][max];
profit += ((a[1][max] / a[0][max]) * (a[0][max]
+ c));
}
}
return profit;
}
int main()
{
int a[2][100], num, weight;
system("cls");
cout << "Enter number of objects: ";
cin >> num;
cout << "Enter the weight of the knapsack: ";
cin >> weight;
cout<<"\nEnter the weight profit of following Items:\n";
for(int i=0;i<num;i++)
{
cout<<"\n=============================Item "<<i+1<<"====================================\n";
cout<<"Weight:";
cin>>a[0][i];
cout<<"Profit :";
cin>>a[1][i];
}
int p=knapsack(a,num,weight);
cout << "\nTotal Profit: " << p<<"\n";
return 0;
}