UrbanPro
true

Learn Advanced Java coaching from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Fractional Knapsack

Paras Chawla
29/05/2017 0 0

Algorithm - Fractional Knapsack

 

import java.util.Scanner;

 

public class Knapsack01 {

     public static void main(String[] args) {

           Scanner scan = new Scanner(System.in);

           int n = scan.nextInt(); // 1

           int W = scan.nextInt(); // 1000

           float value[] = new float[n]; // 500

           float weight[] = new float[n]; // 30

           float ratio[] = new float[n];

           for (int i = 0; i < n; i++) {

                value[i] = scan.nextInt();

                weight[i] = scan.nextInt();

                ratio[i] = value[i] / weight[i];

           }

           float totalValue = knapSack01(W, value, weight, ratio, n);

           System.out.println(totalValue);

     }

 

     public static float knapSack01(float W, float[] value, floatweight[], float ratio[],

                int n) {

           float V = 0;

           while (n>0 && W!=0) {

                int mI = maxIndex(n, ratio);

                float w=min(weight[mI],W);

                // W=40,weight[2]=30 // W=10,weight[0]=20;

                W = W - w;

                V = V + w*ratio[mI];

                weight[mI]=weight[mI]-w;

                ratio[mI] = 0;

                n--;

           }

           return V;

     }

     public static float min(float a,float b){

           return a>b?b:a;

     }

 

     public static int maxIndex(int n, float ratio[]) {

           float largest = Integer.MIN_VALUE;

           int lastIndex = 0;

           for (int i = 0; i < n; i++) {

                if (ratio[i] > largest) {

                     largest = ratio[i];

                     lastIndex = i;

                }

           }

           return lastIndex;

     }

}

 

Output:

Input: 3 50

60 20 100

50 120 30

 

Output: 180.0000

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Java
Java is a high level language which is far better than procedural languages like C, Fortran and Pascal. Java completely stands with the principles of OOPS (Object Oriented Programming Structure). The...
A

Abhisheak Saxena

0 0
0

Big Data Hadoop Training
What is Big Data? Big data means really a big data, it is a collection of large datasets that cannot be processed using traditional computing techniques. Big data is not merely a data, rather it has become...

Importance of Constructor's Visibility.
While developing program or implementing Singleton pattern we have learnt to mention constructor as private and known reason is -> to blocked its direct call from outside of class. But did we think...
M

Maths Home Tutor In Kolkata
Study text book properly and give test regularly.

FBD : Free Body Diagram (Mechanical Engineering)
Free body diagram is the graphical representation of forces (Action and Reaction) acting on isolated body or any objects. it is very basic and most useful concept to solve any structural probleme (can...
X

Looking for Advanced Java coaching Classes?

The best tutors for Advanced Java coaching Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Advanced Java coaching with the Best Tutors

The best Tutors for Advanced Java coaching Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more