HDU 2199(二分求方程解)

关键是确定解的范围以及二分时的判断依据

这里解的范围已经确定,因为是递增函数,二分依据就是和Y的大小比较

#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cmath"
#define MAXN 505
using namespace std;
double cal(double x)
{   double temp=8.0*pow(x,4)+7.0*pow(x,3)+2.0*pow(x,2)+3.0*x+6.0;
    return temp;
}
int main()
{   int T;
    scanf("%d",&T);
    while(T--)
    {   double ans,y;
        scanf("%lf",&y);
        double low=0.0,high=100.0,mid;
        int ok=1;
        if(y>cal(high)||y<cal(low)) ok=0;
        while(ok&&fabs(high-low)>1e-8)
        {   mid=low+(high-low)/2.0;
            if(cal(mid)<y) low=mid;
            else high=mid;
        }
        if(!ok) printf("No solution!
");
        else
            printf("%.4lf
",mid);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/luxiaoming/p/4670993.html