fork download
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cmath>
  4. using namespace std;
  5. int n;
  6. double a[10];
  7. double f(double x)
  8. {
  9. double y=-1;
  10. for(int i=0;i<n;i++)
  11. y*=x-a[i];
  12. return y;
  13. }
  14. double solve(double l,double r)
  15. {
  16. while(r-l>=1e-7)
  17. {
  18. double lm=l+(r-l)/3;
  19. double rm=l+(r-l)/3*2;
  20. if(f(lm)>f(rm)){
  21. r=rm;
  22. }else{
  23. l=lm;
  24. }
  25. }
  26. return l;
  27. }
  28. int main()
  29. {
  30. cin>>n;
  31. for(int i=0;i<n;i++)
  32. cin>>a[i];
  33. int p;
  34. cin>>p;
  35. cout<<fixed<<setprecision(6)<<solve(a[p],a[p+1])<<endl;
  36. }
Success #stdin #stdout 0s 3432KB
stdin
2
46 62 
0
stdout
54.000000