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

HDU 2199 Can you solve this equation?(水题吗?!)

时间:2015-09-03 23:16:45      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description:
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
 
Input:
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
 
Output:
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
 
Sample Input:
2
100
-4
 
Sample Output:
1.6152
No solution!

题意:输入y的值,则求出方程8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y的解,若有解,则该解一定在[0, 100]之间,输出x,若无解则输出No solution!。

#include<stdio.h>
#include<math.h>
int main ()
{
    double a, b, c, y, sum, d;
    int T, x;

    scanf("%d", &T);

    while (T--)
    {
        scanf("%lf", &y);

        x = 0;
        a = 0;
        b = 100;

        d = 8*b*b*b*b + 7*b*b*b + 2*b*b + 3*b + 6;
        c = 8*a*a*a*a + 7*a*a*a + 2*a*a + 3*a + 6;

        if (y >= c && y <= d)
            x = 1;
        if (x)
        {
            while (b-a > 1e-6) ///浮点数判断b是否大于a
            {
                c = (a+b)/2;
                sum = 8*c*c*c*c + 7*c*c*c + 2*c*c + 3*c + 6;

                if (sum > y)
                    b = c-1e-7;
                else
                    a = c+1e-7;
            }

            printf("%.4lf\n", (a+b)/2);
        }

        if (x == 0) printf("No solution!\n");
    }

    return 0;
}

HDU 2199 Can you solve this equation?(水题吗?!)

标签:

原文地址:http://www.cnblogs.com/syhandll/p/4780909.html

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