现在的位置: 首页 > 综合 > 正文

NYOJ 503 & HDU 2199 解方程(二分)

2013年10月05日 ⁄ 综合 ⁄ 共 426字 ⁄ 字号 评论关闭

题目链接:Click
here~~

二分查找。注意精度。

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return 8*pow(x,4) - 7*pow(x,3) + 2*pow(x,2) + 3*x + 6;
}
double Binary_Find(double left,double right,double y)
{
    while(right-left>1e-7)
    {
        double mid=(left+right)/2;
        if(f(mid) < y)
            left  = mid;
        else
            right = mid;
    }
    return left;
}
int main()
{
    int z;
    double y;
    scanf("%d",&z);
    while(z--)
    {
        scanf("%lf",&y);
        if(f(0)>y || f(100)<y)
            puts("No solution!");
        else
            printf("%.4lf\n",Binary_Find(0,100,y));
    }
    return 0;
}

抱歉!评论已关闭.