解题思路
三分,填坑。每次取l与r的中间值mid,然后向左移一点点,向右移一点点进行判断,判断时用秦九韶算法即可。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> using namespace std; const int MAXN = 25; const double eps = 1e-6; int n; double l,r,a[MAXN]; double F(double x){ double sum=0; for(int i=n;i>=0;i--) sum=sum*x+a[i]; return sum; } int main(){ scanf("%d%lf%lf",&n,&l,&r);double mid; for(int i=n;i>=0;i--) scanf("%lf",&a[i]); while(fabs(l-r)>=eps){ mid=(l+r)/2; if(F(mid+eps)>F(mid-eps)) l=mid; else r=mid; } printf("%.5lf",r); return 0; }