Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool cmp(Item&a, Item&b){
- double r1 = (a.value * 1.0)/(a.weight*1.0);
- double r2 = (b.value * 1.0)/(b.weight*1.0);
- return r1 > r2;
- }
- // function to return fractionalweights
- double fractionalKnapsack(int W, Item arr[], int n)
- {
- // Your code here
- double ans = 0;
- sort(arr, arr+n, cmp);
- for(int i=0; i<n; i++){
- if(W >= arr[i].weight){
- ans+=arr[i].value;
- W-=arr[i].weight*1.0;
- }
- else{
- ans += ((W*1.0)/(arr[i].weight * 1.0)) * (1.0 * arr[i].value);
- break;
- }
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement