Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Fractional Knapsack problem

#include<bits/stdc++.h>
using namespace std;

float p[100],w[100];
float x[100];

    void knapsack(int m,int n)
    {
        for(int i = 1; i<n; i++)
        {
            int maX = i;
            for(int j =i+1;j<=n;j++)
            {
                if(p[j]/w[j] > p[maX]/w[maX])
                {
                    maX = j;
                }
            }
            swap(p[maX],p[i]);
            swap(w[maX],w[i]);

        }

        int u = m;
        int i;
        for(i=1;i<=n;i++)
        {
            if(w[i]>u)
                break;
            x[i] = 1;
            u = u-w[i];
        }
        if(i<=n)
        {
            x[i] = u/w[i];
        }
    }


    void profit_calculation(int n)
    {
        float sum = 0;
        for(int i = 1;i<=n;i++)
            sum += x[i]*p[i];
        cout<<sum;
    }


int main()
{

   int n;    // n is the number of items
    cout<<"Enter the no. of items:";
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        cin>>p[i]>>w[i];  // Enter Profit space Weight respectively

    }
    cout<<endl;
    knapsack(60,4);
    cout<<"After Sorting according to unit profit: "<<endl;
    cout<<"profit: ";
    for(int i = 1;i<=n;i++)
        cout<<p[i]<<" ";
    cout<<endl<<"Weight: ";
    for(int i = 1;i<=n;i++)
        cout<<w[i]<<" ";
    cout<<endl;

    cout<<"You can take the items respectively portion of:"<<endl;
    for(int i = 1;i<=n;i++)
        cout<<x[i]<<" ";
    cout<<endl;
    cout<<"your total profit is: ";
    profit_calculation(n);



    return 0;
}
Comment

Fractional Knapsack


/*
class Item {
    int value, weight;
    Item(int x, int y){
        this.value = x;
        this.weight = y;
    }
}
*/

class CompareElements implements Comparator<Item>
{
    @Override
    public int compare(Item a, Item b)
    {
        double val1= (double)a.value/a.weight;
        double val2= (double)b.value/b.weight;
        
        if(val1>val2)
        return -1;
        
        else if(val2>val1)
        return 1;
        
        else
        return 0;
    }
}

class Solution
{
    //Function to get the maximum total value in the knapsack.
    double fractionalKnapsack(int W, Item arr[], int n) 
    {
        Arrays.sort(arr, new CompareElements());
        
        double currentValue=0;
        
        for(int i = 0 ; i<n;i++)
        {
            if(arr[i].weight<=W)
            {
                currentValue+=arr[i].value;
                W-=arr[i].weight;
            }
            else
            {
                currentValue+= ((double)arr[i].value/(double)arr[i].weight)*(double)W;
                break;
            }
        }
        return currentValue;
    }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: heap allocated array in c ++ 
Cpp :: abs in c++ used for 
Cpp :: C++ Vector Operation Change Elements 
Cpp :: know what the input data is whether integer or not 
Cpp :: kadane algorithm with negative numbers included as sum 
Cpp :: vector size c++ 
Cpp :: print all subsequences 
Cpp :: file streams in c++ 
Cpp :: char at in c++ 
Cpp :: c++ stl 
Cpp :: how to compile c++ code with g+ 
Cpp :: C/C++ loop for 
Cpp :: Consell de forces polítiques de Catalunya 
Cpp :: c++c 
Cpp :: pointers in cpp 
Cpp :: code runner c++ syntax error 
Cpp :: c++ to c converter tool 
Cpp :: sort array in descending order c++ 
Cpp :: cpp how to add collisions to boxes 
Cpp :: how to signify esc key in cpp 
Cpp :: Calculating Function codeforces in c++ 
Cpp :: reverse a stack in c++ using another stack 
Cpp :: digits in c++ 
Cpp :: how to open program in c++ 
Cpp :: unreal engine c++ bind action to function with parameter 
Cpp :: c/c++ pointers 
Cpp :: punteros a arrays 
Cpp :: output sum of a range 
Cpp :: displaying m images m windows opencv c++ 
Cpp :: swift functions from cpp 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =