SDUST - Training F HDU2199 方程求解,二分查找,精度控制

版权声明:转载请注明出处 https://blog.csdn.net/pangel18/article/details/46411229

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!
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while(n--){
        bool f=true;
        double y;
        cin >> y;
        double a=0, b=100;
        if(y<6 || y>8*100*100*100*100+7*100*100*100+2*100*100+3*100+6){
            cout << "No solution!" << endl;
            continue;
        }
        while(fabs(a-b)>=1e-7){
//            cout << "-" << endl;
            double t=(a+b)/2;
            if(fabs(8*t*t*t*t+7*t*t*t+2*t*t+3*t+6-y)==0){
//                cout << "=" << endl;
                cout << fixed << setprecision(4) << t << endl;
                f=false;
                break;
            }
            else if(8*t*t*t*t+7*t*t*t+2*t*t+3*t+6>y){
                b=t;
            }
            else a=t;
//            cout << a << " " << b << " " << t << endl;
        }
        if(f==true) cout << fixed << setprecision(4) << (a+b)/2 << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/pangel18/article/details/46411229