fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7.  
  8. static double LP(double x, double[] xv, double[] yv){
  9. int size = xv.length;
  10. double sum = 0;
  11. for(int i = 0; i < size; i++){
  12. double mul = 1;
  13. for(int j = 0; j < size; j++){
  14. if(i != j){
  15. mul *= (x-xv[j])/(xv[i]-xv[j]);
  16. }
  17. }
  18. sum += yv[i]*mul;
  19. }
  20. return sum;
  21. }
  22.  
  23. static Scanner sc = new Scanner(System.in);
  24.  
  25. public static void main (String[] args) throws java.lang.Exception
  26. {
  27. int counter = 0;
  28. double a = -3;
  29. double b = 3;
  30. int fragments = 101;
  31. ArrayList<Double> xvinp = new ArrayList<Double>();
  32. ArrayList<Double> yvinp = new ArrayList<Double>();
  33. double[] xv;
  34. double[] yv;
  35. while(sc.hasNext()){
  36. double inp = sc.nextDouble();
  37. if(counter%2 == 0){
  38. xvinp.add(inp);
  39. }else{
  40. yvinp.add(inp);
  41. }
  42. counter++;
  43. }
  44. xv = new double[xvinp.size()];
  45. yv = new double[yvinp.size()];
  46. for(int i = 0; i < xvinp.size(); i++){
  47. xv[i] = xvinp.get(i);
  48. yv[i] = yvinp.get(i);
  49. }
  50. for(int i = 0; i < fragments; i++){
  51. double x = a + (b-a)/(fragments-1)*i;
  52. System.out.println(x + "\t" + LP(x, xv, yv));
  53. }
  54. }
  55. }
Success #stdin #stdout 0.18s 321408KB
stdin
-1 1 	0 2 1 3 2 -1
stdout
-3.0	19.0
-2.94	17.786820000000002
-2.88	16.626559999999998
-2.82	15.518139999999992
-2.76	14.460479999999993
-2.7	13.4525
-2.64	12.493120000000001
-2.58	11.581260000000002
-2.52	10.715840000000002
-2.46	9.895779999999997
-2.4	9.120000000000005
-2.34	8.387419999999997
-2.2800000000000002	7.696960000000004
-2.2199999999999998	7.047539999999996
-2.16	6.438080000000003
-2.1	5.8675
-2.04	5.334719999999999
-1.98	4.838660000000001
-1.92	4.37824
-1.86	3.952380000000002
-1.8	3.5600000000000014
-1.74	3.2000200000000025
-1.6800000000000002	2.871360000000001
-1.62	2.572940000000001
-1.56	2.303680000000001
-1.5	2.0625
-1.44	1.8483199999999995
-1.3800000000000001	1.6600600000000008
-1.32	1.49664
-1.26	1.3569800000000003
-1.2000000000000002	1.2400000000000002
-1.1400000000000001	1.1446199999999997
-1.08	1.06976
-1.02	1.01434
-0.96	0.97728
-0.8999999999999999	0.9575000000000001
-0.8399999999999999	0.95392
-0.7800000000000002	0.96546
-0.7200000000000002	0.99104
-0.6600000000000001	1.02958
-0.6000000000000001	1.0799999999999996
-0.54	1.1412200000000001
-0.48	1.2121600000000001
-0.41999999999999993	1.29174
-0.3600000000000003	1.3788799999999994
-0.30000000000000027	1.4724999999999997
-0.2400000000000002	1.5715199999999996
-0.18000000000000016	1.6748599999999996
-0.1200000000000001	1.7814399999999995
-0.06000000000000005	1.8901799999999997
0.0	2.0
0.06000000000000005	2.10982
0.1200000000000001	2.21856
0.17999999999999972	2.3251399999999993
0.23999999999999977	2.42848
0.2999999999999998	2.5275
0.3599999999999999	2.62112
0.41999999999999993	2.7082599999999997
0.48	2.7878399999999997
0.54	2.8587800000000003
0.5999999999999996	2.9199999999999995
0.6599999999999997	2.97042
0.7199999999999998	3.00896
0.7799999999999998	3.0345400000000002
0.8399999999999999	3.04608
0.8999999999999999	3.0425
0.96	3.0227200000000005
1.0199999999999996	2.98566
1.08	2.93024
1.1399999999999997	2.855380000000001
1.2000000000000002	2.76
1.2599999999999998	2.643020000000001
1.3200000000000003	2.5033599999999994
1.38	2.3399400000000004
1.4399999999999995	2.1516800000000016
1.5	1.9375
1.5599999999999996	1.6963200000000018
1.62	1.42706
1.6799999999999997	1.1286400000000014
1.7400000000000002	0.7999799999999985
1.7999999999999998	0.44000000000000083
1.8599999999999994	0.047620000000004215
1.92	-0.37823999999999947
1.9799999999999995	-0.8386599999999963
2.04	-1.3347200000000004
2.0999999999999996	-1.8674999999999966
2.16	-2.4380800000000016
2.2199999999999998	-3.0475399999999975
2.2799999999999994	-3.6969599999999927
2.34	-4.387419999999999
2.3999999999999995	-5.119999999999994
2.46	-5.8957799999999985
2.5199999999999996	-6.715839999999995
2.58	-7.58126
2.6399999999999997	-8.493119999999994
2.7	-9.452500000000004
2.76	-10.460479999999997
2.8199999999999994	-11.518139999999988
2.88	-12.626559999999994
2.9399999999999995	-13.786819999999988
3.0	-15.0