-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.cpp
More file actions
56 lines (54 loc) · 1.1 KB
/
Knapsack.cpp
File metadata and controls
56 lines (54 loc) · 1.1 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
#include<bits/stdc++.h>
#define loop(i,n) for(int i=0;i<n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
struct ob
{
float value;
float weight;
};
bool comp(ob x,ob y)
{
float r1=x.value/x.weight;
float r2=y.value/y.weight;
return r1>r2;
}
void fractional_knapsack(ob A[],int n,float capacity)
{
sort(A,A+n,comp);
float cw=0.0,final_value=0.0;
loop(i,n)
{
if(cw+A[i].weight<=capacity)
{
cw+=A[i].weight;
final_value+=A[i].value;
}
else
{
float remain=capacity-cw;
final_value+=A[i].value*(remain/A[i].weight);
break;
}
}
cout<<"the ans is "<<fixed<<setprecision(2)<<final_value<<endl;
}
int main()
{
//fast;
int n;
float capacity;
cout<<"How many product you have: ";
cin>>n;
ob A[n+5];
loop(i,n)
{
cout<<"product "<<i+1<<" value: ";
cin>>A[i].value;
cout<<"product "<<i+1<<" weight: ";
cin>>A[i].weight;
}
cout<<"enter the maximum capacity: ";
cin>>capacity;
fractional_knapsack(A,n,capacity);
}