首页 > 代码库 > hdu 2199 Can you solve this equation?(二分法求多项式解)

hdu 2199 Can you solve this equation?(二分法求多项式解)

题意

给Y值,找到多项式 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y 在0到100之间的解。

 

思路

从0到100,多项式是单调的,故用二分法求解。

 

代码

double calc(double x){    return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;}int main(){    int T;    cin>>T;    while(T--){        double Y;        cin>>Y;        double L,R;        L = 0.0,  R= 100.0;        if(calc(L)>Y || Y>calc(R)){            cout<<"No solution!"<<endl;        }else{            double mid;            while((R-L)>(1e-10)){                mid = (L+R) / 2;                if(calc(mid)>Y){                    R = mid;                }else{                    L = mid;                }            }            mid = (R+L)/2;            printf("%.4lf\n",mid);        }    }    return 0;}

 

hdu 2199 Can you solve this equation?(二分法求多项式解)