-
Notifications
You must be signed in to change notification settings - Fork 4
/
FKnapSack.java
93 lines (90 loc) · 2.04 KB
/
FKnapSack.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.*;
public class FKnapSack
{
public static float[] fknapcal(int w[],float p[],int W)
{
int n=p.length;
float x[]=new float[n];
for(int i=0;i<n;i++)
{
x[i]=0;
}
int weight=0;
for(int i=0;i<n;i++)
{
if(weight+w[i]<=W)
{
x[i]=1;
weight=weight+w[i];
}
else
{
x[i]=(float)(W-weight)/w[i];
weight=W;
break;
}
}
return x;
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the no of items:");
int n=in.nextInt();
int v[]=new int[n];
int w[]=new int[n];
System.out.println("Enter the value Array:");
for(int i=0;i<v.length;i++)
{
v[i]=in.nextInt();
}
System.out.println("Enter weight Array:");
for(int i=0;i<v.length;i++)
{
w[i]=in.nextInt();
}
System.out.println("Enter total capacity:");
int W=in.nextInt();
float p[]=new float[n];
for(int i=0;i<n;i++)
{
p[i]=(float)v[i]/w[i];
}
SortRatio(p,v,w);
float x[]=new float[n];
for(int i=0;i<n;i++)
{
x=fknapcal(w,p,W);
}
for(int i=0;i<n;i++)
System.out.println(x[i]);
}
public static void SortRatio(float p[], int v[], int w[])
{
float n=p.length;
float temp=0,flag=0;
for(int i=0;i<n;i++)
{
for(int j=1;j<(n-i);j++)
{
if(p[j-1]<p[j])
{
temp=p[j-1];
p[j-1]=p[j];
p[j]=temp;
temp=v[j-1];
v[j-1]=v[j];
v[j]=(int)temp;
temp=w[j-1];
w[j-1]=w[j];
w[j]=(int)temp;
flag++;
}
}
if(flag==0)
{
break;
}
}
}
}