fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.math.*;
  5.  
  6. class Ideone
  7. {
  8. public static double c_price(double p, double p1, double s1, double p2, double s2){
  9. if(p1 == p2){ //if given solutions' concentrations are equal
  10. return p1 == p ? Math.min(s1, s2) : 0;
  11. }
  12. if(p1 == p || p2 == p){ //if some given concentration equals to needed
  13. return p1 == p ? s1 : s2;
  14. }
  15. double a1 = (p-p1)/(p2-p1);
  16. if(a1 > 1 || a1 < 0) return 0; //inconsistent data
  17. double a2 = 1-a1;
  18. return s1*a1 + s2*a2;
  19. }
  20. public static void main (String[] args) throws java.lang.Exception
  21. {
  22. Scanner in = new Scanner(System.in);
  23. double[] p = new double[3];
  24. double[] s = new double[3];
  25. for(int i = 0; i < 3; i++) p[i] = in.nextDouble();
  26. double f_p = in.nextDouble();
  27. for(int i = 0; i < 3; i++) s[i] = in.nextDouble();
  28. double s12 = c_price(f_p, p[0], s[0], p[1], s[1]);
  29. double s13 = c_price(f_p, p[0], s[0], p[2], s[2]);
  30. double s23 = c_price(f_p, p[1], s[1], p[2], s[2]);
  31. if(s[0] == 0 && s[1] == 0 && s[2] == 0)
  32. System.out.println("Impossible");
  33. else{
  34. double min_price = s12;
  35. if(s13 != 0) min_price = Math.min(min_price, s13);
  36. if(s23 != 0) min_price = Math.min(min_price, s23);
  37. System.out.printf("%.2f", min_price);
  38. }
  39. }
  40. }
Success #stdin #stdout 0.16s 321344KB
stdin
0	0	0	0
10	12	14
stdout
10.00