HDOJ 2199 Can you solve this equation?

来源:互联网 发布:网络语柚子是什么意思 编辑:程序博客网 时间:2024/06/05 17:53

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


题目大意:有一个方程8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,现在给定Y,求x的近似值,结果保留4位小数。


题目思路:很容易看出来,这个函数是单调递增函数,所以我们直接二分答案。


AC代码:

//  HDOJ//  2199.cpp/*    ID: Firwaless    LANG: C++    TASK: Can you solve this equation?*/#include <cstdio>#include <cmath>#define eps 1e-10double value(double x){    return 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6;}int main(){    int n;    double x, y, l, r, mid;        while (~scanf("%d", &n))    {        while (n--)        {            scanf("%lf", &y);            l = 0;            r = 100;            if (value(0) > y || value(100) < y)            {                puts("No solution!");            }            else            {                while (r - l > eps)                {                    mid = (l + r) / 2;                    x = value(mid);                    if (x < y)                    {                        l = mid + eps;                    }                    else                    {                        r = mid - eps;                    }                }                printf("%.4lf\n", mid);            }        }    }    return 0;}


0 0
原创粉丝点击