fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. /*d=7, n=2, s=6, e=7, Output is: 7.0000000000
  6. d=5, n=3, s=5, e=5, Output is: 2.5000000006
  7. d=3, n=3, s=0, e=1, Output is: 1.5000000007
  8. d=9, n=10, s=5, e=6, Output is: 1.0000000000
  9. d=6, n=2, s=1, e=6, Output is: 6.0000000000
  10. d=5, n=3, s=4, e=5, Output is: 2.5000000006*/
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. func(7, 2, 6, 7);
  16. func(5, 3, 5, 5);
  17. func(3, 3, 0, 1);
  18. func(9, 10, 5, 6);
  19. func(6, 2, 1, 6);
  20. func(5, 3, 4, 5);
  21. }
  22.  
  23. static void func(double d, int n, double s, double e)
  24. {
  25. double high = d, low = 0, epsilon = 0.000001;
  26. while (low + epsilon < high)
  27. {
  28. double mid = (low + high)/2;
  29. int gapsOnLeft = (int)(s/mid);
  30. if (gapsOnLeft + 1 > n)
  31. gapsOnLeft = n - 1;
  32. int gapsOnRight = n - gapsOnLeft - 2;
  33. double leftOffset = mid*gapsOnLeft;
  34. double rightOffset = d - mid*gapsOnRight;
  35. if (leftOffset + mid <= rightOffset && rightOffset >= e)
  36. low = mid;
  37. else
  38. high = mid;
  39. }
  40. System.out.println(low);
  41. }
  42. }
Success #stdin #stdout 0.05s 4386816KB
stdin
Standard input is empty
stdout
6.999999165534973
2.5
1.5
0.9999999403953552
5.999999284744263
2.5