其實很簡單,就是每次看看此點左右的哪一個大,貪心地選:c++
#include<bits/stdc++.h> using namespace std; const double eps=1e-7; const int N=16; int n; double t[N],l,r; double f(double x){ double ans=0; for(int i=0;i<=n;++i) ans=ans*x+t[i]; } int main(){ scanf("%d%lf%lf",&n,&l,&r); for(int i=0;i<=n;++i) scanf("%lf",&t[i]); while(fabs(r-l)>=eps){ double mid=(l+r)/2; if(f(mid+eps)>f(mid-eps)) l=mid; else r=mid; } printf("%.5lf",r); return 0; }