码迷,mamicode.com
首页 > 其他好文 > 详细

hdu Can you solve this equation?

时间:2015-03-07 22:41:56      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

本题是一道二分的题,核心就是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

 

hdu Can you solve this equation?

标签:

原文地址:http://www.cnblogs.com/acm-jing/p/4321021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!