HDU2199 Can you solve this equation?(二分)

来源:互联网 发布:美国零售销售数据公布 编辑:程序博客网 时间:2024/05/21 17:06

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199

二分最简单的应用

#include<stdio.h>#include<math.h>double cal(double x){    return  8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        double n;        scanf("%lf",&n);        if(cal(0)>n||cal(100)<n)        {            printf("No solution!\n");            continue;        }        double l=0.0,r=100.0;        double mid=(l+r)/2;        while(fabs(cal(mid)-n)>1e-5)        {            if(cal(mid)>n)                r=mid;            else                l=mid;            mid=(l+r)/2;        }        printf("%.4lf\n",mid);    }    return 0;}


2 0