diff --git a/dynamic_programing/fractional_knapsack/java/Fractional.java b/dynamic_programing/fractional_knapsack/java/Fractional.java new file mode 100644 index 0000000..0eb8920 --- /dev/null +++ b/dynamic_programing/fractional_knapsack/java/Fractional.java @@ -0,0 +1,67 @@ +/* +fractional Knapsack +3 +50 +60 10 +100 20 +120 30 +Ans is 240.0 +*/ +import java.util.*; +class Fractional{ + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + ArrayList al=new ArrayList(); + System.out.println("Enter no of Element:"); + int n=sc.nextInt(); + System.out.println("Enter Maxium waight:"); + int weight=sc.nextInt(); + int a[]=new int[n]; + for (int i=0;i=0) { + sum=sum+al.get(i).p; + weight-=al.get(i).w; + System.out.println("weight "+weight+" price "+al.get(i).p); + } + else { + sum+=((al.get(i).p)*((double)weight/al.get(i).w)); + double x=((al.get(i).p)*((double)weight/al.get(i).w)); + System.out.println("weight "+weight+" price1 "+x); + weight-=al.get(i).w*(((double)weight/al.get(i).w)); + } + } + System.out.println("Maximum Profit is -> "+sum); + + } +} +class pair implements Comparable +{ + int p,w; + double f;//sorting all pairs with fractinal parameter + pair(int p,int w,double f) + { + this.p=p; + this.w=w; + this.f=f; + } + public String toString(){ + return this.p+" "+this.w+" "+this.f; + } + public int compareTo(pair pa) + { + //return -(int)(this.f-pa.f);//here is compare the values like 1.6 and 1.5 + if (this.f>pa.f) return -1; + if (this.fa[i-1][j-1]) + { + sb.append(x[i-1]); + --i;--j; + } + else if(a[i][j-1]==a[i-1][j] &&a[i][j]==a[i-1][j-1]) + { + --i; + } + else if(a[i][j-1]>a[i-1][j]) + { + --j; + } + else{ + --i; + } + } + System.out.println(); + + System.out.print("LCS Lenght :-> "+a[n][m]); + System.out.println(); + + System.out.print("LCS is:-> "); + System.out.println(sb.reverse()); + } +} \ No newline at end of file