Can you solve this equation?

来源:互联网 发布:淘宝 发货地不符 编辑:程序博客网 时间:2024/05/16 10:40
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;<br>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<br>100<br>-4<br>
 

Sample Output
1.6152<br>No solution!<br>
 

Author
Redow

题目大意:

一个多项式,给出y值,让你求x的值。

思路:

将题目看作f(x) = 8*x^47*x^3 2*x^2 3*x 6, x∈[0, 100]时,可以发现整个函数都是递增的,用二分法即可解题

感想:

其实就是一道中学代数题,因为有思路,所以不难,具体就是二分法来做,有引文x<-[0,100]是递增的,所以有规律

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
double f(double x)
{
    return (8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6);
}
using namespace std;
int main()
{
    int t;
    cin >> t;
    for (int i = 0;i<t;i++)
    {
        double y, mid;
        cin >> y;
        if (f(0)>y || f(100)<y)cout << "No solution!" << endl;
        else
        {
            double left = 0, right = 100;
            while (right - left>1e-10)
            {
                mid = (right + left) / 2;
                if (f(mid)>y)
                    right = mid;
                else left = mid;
            }
            printf("%.4f\n", mid);
        }
    }
    return 0;
}

0 0