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

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

时间:2015-09-04 15:32:54      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/zhengguiping--9876/p/4781658.html

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