Can you solve this equation?(二分)

版权声明:转载请注明出处链接 https://blog.csdn.net/qq_43408238/article/details/89737160

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!

         二分水题,虽然过了,但还是有点迷,就是在验证时等号的归属问题还有待思考。一开始的精度和答案有些差别,我又把eps缩了两位。

AC Code

#include<iostream>
#include<iomanip>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#define PI  3.14159265358979
#define LL long long
#define  eps   0.00000001
using namespace std;
double ans[10010];
 int n,m,t;
double cal(double x)
{
    return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}
int main()
{
   // freopen("input.txt","r",stdin);
    int T,n,m;
    cin>>T;
    while(T--)
    {
        cin>>n;
        if(n<cal(0)||n>cal(100)) {
            cout<<"No solution!"<<endl;continue;
        }
        double L=0,H=100;double mid;
        while(fabs(H-L)>eps)
        {
            mid=(H+L)/2;
            if(cal(mid)>=n) {
                H=mid;
            }
            else L=mid;
        }
        cout<<fixed<<setprecision(4)<<mid<<endl;

    }

}

猜你喜欢

转载自blog.csdn.net/qq_43408238/article/details/89737160
今日推荐