Can you solve this equation?---hdu2199(二分)

http://acm.hdu.edu.cn/showproblem.php?pid=2199

给出y的值求x;

8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = Y

x是0到100的实数所以要用二分的方法;

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
#define N 110
#define INF 0xffffff

int main()
{
    int T,flag;
    double y, m, L, R, Mid;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lf", &y);
        m=8*pow(100,4)+7*pow(100,3)+2*pow(100,2)+3*100+6;
        if(m<y||y<6)
        {
            printf("No solution!\n");
            continue;
        }
        L=0;R=100;
        while(L<=R)
        {
            Mid=(L+R)/2;
            m=8*pow(Mid,4)+7*pow(Mid,3)+2*pow(Mid,2)+3*Mid+6;
            if(fabs(m-y)<0.0001)
            {
                flag=1;
                break;
            }
            else if(m>y)
            {
                R=Mid;
            }
            else
            {
                L=Mid;
            }
        }
        if(flag==1)
            printf("%.4lf\n", Mid);
        else
            printf("No solution!\n");
    }
    return 0;
}
View Code

 

posted @ 2015-09-04 13:56  西瓜不懂柠檬的酸  Views(140)  Comments(0)    收藏  举报
levels of contents