-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
57 lines (44 loc) · 1.58 KB
/
Knapsack.java
File metadata and controls
57 lines (44 loc) · 1.58 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
57
import java.util.*;
public class Knapsack {
static float knapsack(float[] weight, float[] profit, int n, float m){
float sum=0, max;
int j=0;
while(m > 0){
max=0;
for (int i = 0; i < n; i++) {
if((profit[i] / weight[i]) > max){
max = profit[i] / weight[i];
j=i;
}
}
if(weight[j] > m){
System.out.println("Quantity of item number " +(j+1)+ " added is "+ m);
sum = sum + (m*(profit[j] / weight[j]));
break;
}
else{
System.out.println("Quantity of item number " +(j+1)+ " added is "+ weight[j]);
m = m - weight[j];
sum = sum + profit[j];
profit[j]=0;
}
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of items : ");
int n = sc.nextInt();
float[] weight = new float[n];
float[] profit = new float[n];
System.out.println("Enter the weights and profits of the Knapsack : ");
for (int i = 0; i < n; i++) {
weight[i] = sc.nextInt();
profit[i] = sc.nextInt();
}
System.out.println("Enter the maximum volume of the Knapsack : ");
float m = sc.nextInt();
float result = knapsack(weight, profit, n, m);
System.out.println("The total profit is : "+ result);
}
}