-
Notifications
You must be signed in to change notification settings - Fork 0
/
FractionalKnapsack.java
40 lines (40 loc) · 1.42 KB
/
FractionalKnapsack.java
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
import java.util.*;
class Item {
int weight;
int value;
Item(int weight, int value) {
this.weight = weight;
this.value = value;
}
}
public class FractionalKnapsack {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of items: ");
int n = sc.nextInt();
Item[] items = new Item[n];
System.out.println("Enter weight and value of each item:");
for (int i = 0; i < n; i++) {
int weight = sc.nextInt();
int value = sc.nextInt();
items[i] = new Item(weight, value);
}
System.out.print("Enter the maximum weight of the knapsack: ");
int maxWeight = sc.nextInt();
Arrays.sort(items, (a, b) -> Double.compare((double) b.value / b.weight, (double) a.value / a.weight));
double totalValue = 0.0;
int currentWeight = 0;
for (Item item : items) {
if (currentWeight + item.weight <= maxWeight) {
totalValue += item.value;
currentWeight += item.weight;
} else {
int remainingWeight = maxWeight - currentWeight;
totalValue += item.value * ((double) remainingWeight / item.weight);
break;
}
}
System.out.println("Maximum value achievable in the knapsack: " + totalValue);
sc.close();
}
}