首页 > 技术文章 > hdu Can you solve this equation?

acm-jing 2015-03-07 22:13 原文

本题是一道二分的题,核心就是mi的大小,即精度的取值。因为原函数是一个单调递增的函数,所以可以确定y的取值范围,并且在范围内的每一个y的值,一定至少存在一个x与其对应。刚开始我将取二分这个环节用一个函数来表示的,但是返回值始终是一个随机值,实在是搞不懂,无奈之下只能将那个步骤直接写在主函数内。。。

#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"cmath"
#define mi 1e-8
using namespace std;
double cf(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;
        if(y<6 || y>807020306)
        {
            printf("No solution!\n");
            continue;
        }
       double x1=0.0,x2=100.0;
       while(x2-x1>mi)
       {
           double x=(x2+x1)/2.0;
           if(cf(x)>y) x2=x;
           else x1=x;
       }
        printf("%.4lf\n",x1);
    }
    return 0;
}
View Code

 

推荐阅读