forked from manavdoda7/CPP-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractional_Knapsack.cpp
More file actions
87 lines (80 loc) · 1.87 KB
/
Fractional_Knapsack.cpp
File metadata and controls
87 lines (80 loc) · 1.87 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
75
76
77
78
79
80
81
82
83
84
85
86
87
// FRACTIONAL KNAPSACK
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct item
{
int id;
double val;
double weight;
};
// sorting the items in the decreasing order of their (val/weight) ratio
void sort(struct item a[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1; j++)
{
double x = a[j].val / a[j].weight;
double y = a[j + 1].val / a[j + 1].weight;
if (y > x)
{
int temp = a[j].id;
a[j].id = a[j + 1].id;
a[j + 1].id = temp;
temp = a[j].val;
a[j].val = a[j + 1].val;
a[j + 1].val = temp;
temp = a[j].weight;
a[j].weight = a[j + 1].weight;
a[j + 1].weight = temp;
}
}
}
}
void maximise(struct item a[], int n, int w)
{
sort(a, n);
int u = w;
double res = 0.0;
int i;
for (i = 0; i < n; i++)
{
if (a[i].weight <= u)
{
cout << a[i].id << " " << a[i].val << " " << a[i].weight << endl;
u = u - a[i].weight;
res += a[i].val;
}
else
break;
}
if (i < n)
{
cout << a[i].id << " " << a[i].val << " " << a[i].weight;
res += ((u / a[i].weight) * a[i].val);
}
cout << "\nMAXIMUM IS: " << res;
}
// driver function
int main()
{
int n;
cout << "ENTER NUMBER OF ITEMS: ";
cin >> n;
int capacity;
item arr[n];
cout << "Enter the item no., value and weight of the each item respectively"
<< "\n";
for (int i = 0; i < n; i++)
{
cin >> arr[i].id;
cin >> arr[i].val;
cin >> arr[i].weight;
}
cout << "Enter the capacity of the bag"
<< "\n";
cin >> capacity;
maximise(arr, n, capacity);
return 0;
}