首页 > 其他 > 详细

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

时间:2015-09-03 23:16:30      阅读:274      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!