hdu 2199 Can you solve this equation?

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;

double y;
double fcn(double x)
{
    double t=8*x*x*x*x+7*x*x*x+2*x*x+3*x+6-y;
    return t;
}

int main()
{
    int t;
    double x1,x2,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf",&y);
        x1=0,x2=100;

        if(fcn(x1)>0&&fcn(x2)>0)
        {
            printf("No solution!\n");
            continue;
        }
        if(fcn(x1)<0&&fcn(x2)<0)
        {
            printf("No solution!\n");
            continue;
        }

        if(fcn(x1)==0)
        {
            printf("%.4f\n",x1);
            continue;
        }
        if(fcn(x2)==0)
        {
            printf("%.4f\n",x2);
            //printf("%.2lf\n",fcn(x2));
            continue;
        }
        int f=0;
        while(x2-x1>1e-10)
        {
            x=(x1+x2)/2;
            double t1,t2,t3;
            t1=fcn(x1);
            t2=fcn(x);
            t3=fcn(x2);
            if(fcn(x1)==0)
            {
                f=1;
                break;
            }
            if(fcn(x2)==0)
            {
                f=2;
                break;
            }
            if(t1>0&&t2<0) x2=x;
            else if(t1<0&&t2>0) x2=x;
            else if(t2>0&&t3<0) x1=x;
            else if(t2<0&&t3>0) x1=x;
            //printf("%.4f\n",fcn(x1));
        }
        if(f==1) printf("%.4f\n",x1);
        else printf("%.4f\n",x2);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-05-02 08:15  xryz  阅读(120)  评论(0编辑  收藏  举报