forked from izzarzn/cselab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6B.java
76 lines (75 loc) · 1.66 KB
/
6B.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.Scanner;
class Greedy
{
float w;
float p;
float r;
}
public class Kobject {
static final int max = 20;
static int n; static int i; static int r;
static float m;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of objects");
n = input.nextInt();
Greedy[]obj = new Greedy[n];
for(i=0; i<n; i++)
obj[i] = new Greedy();
ReadObject (obj);
knapsack(obj);
}
static void ReadObject(Greedy obj[])
{
Greedy temp = new Greedy();
Scanner input = new Scanner(System.in);
System.out.println("Enter the maximum capacity of KnapSack");
m = input.nextFloat();
System.out.println("Enter weight");
for(i=0; i<n; i++) {
obj[i].w = input.nextFloat();
}
System.out.println("Enter Profit");
for(i=0; i<n; i++) {
obj[i].p = input.nextFloat();
}
for(i=0; i<n; i++) {
obj[i].r = obj[i].p / obj[i].w;
}
for(i=0; i<n-1; i++) {
for(int j=0; j<n-1-i; j++) {
if(obj[i].r < obj[j+1].r) {
temp = obj[j];
obj[j] = obj[j+1];
obj[j+1] = temp;
} } } }
static void knapsack(Greedy obj[])
{
float x[] = new float[max];
float totalprofit;
int i;
float rc;
rc = m;
totalprofit = 0;
for(i=0; i<n; i++){
x[i] = 0;
for(i=0; i<n; i++)
{
if(obj[i].w > rc) {
break;
}
else
{
x[i] = 1;
totalprofit = totalprofit + obj[i].p;
rc = rc - obj[i].w;
} }
System.out.println("i" + i);
if(i<=n)
x[i] = rc/obj[i].w;
totalprofit = totalprofit + obj[i].p * x[i];
System.out.println("The Solution vector , x[]");
for(i=0; i<n; i++)
System.out.println(x[i] + " ");
System.out.println("\n TotalProfit = " + totalprofit);
} } }