-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractknapsack.cpp
69 lines (56 loc) · 1.2 KB
/
fractknapsack.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct item{
int w, v;
float r;
};
float fractional_knapsack(int n, int w, struct item items[]){
int i=n-1;
float max=0, curr=0;
while(i>=0 && curr<w){
if(items[i].w+curr<=w){
max+=items[i].v;
curr+=items[i].w;
}
else{
max+=(((w-curr)/items[i].w)*items[i].v);
curr+=(((w-curr)/items[i].w)*items[i].w);
}
i--;
}
return max;
}
void insertion(int n, struct item items[]){
int i, j;
struct item key;
for(i=0; i<n; i++){
key=items[i];
j=i-1;
while(j>=0 && items[j].r>key.r){
items[j+1]=items[j];
j--;
}
items[j+1]=key;
}
}
int main(){
int n, i, w;
cout<<"Enter No of Items :: ";
cin>>n;
cout<<"Enter Maximum Weight :: ";
cin>>w;
struct item items[n];
for(i=0; i<n; i++){
cout<<"Enter Weight & Value of "<<i+1<<" item :: ";
cin>>items[i].w>>items[i].v;
items[i].r=items[i].v/items[i].w;
}
insertion(n, items);
cout<<"Sorted Items Are :: \n";
for(i=0; i<n; i++){
cout<<"\nItem "<<i+1<<" is (W, V, R) :: ";
cout<<items[i].w<<" "<<items[i].v<<" "<<items[i].r;
}
cout<<"\nMax Profit is :: "<<fractional_knapsack(n, w, items);
return 0;
}