HDOJ 2199 Can you solve this equation

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的x
通过二分查找
最后精确位数为0.000001

CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <time.h>
using namespace std;
double f(double x,int n)
{
    double sum=1;
    for(int i=0;i<n;i++)
        sum*=x;
    return sum;
}
int main()
{
    int n;
    double mid,m;
    cin >> n;
    while(n--)
    {
        double mid = 100;
        //printf("%lf\n",8*f(mid,4)+7*f(mid,3)+2*f(mid,2)+3*f(mid,1)+6);
        cin >> m;
        if(m<6||m>807020306)
        {
            printf("No solution!\n");
            continue;
        }
        double l=0,r=100;
        while(r-l>0.000001)
        {
            mid = (l+r)/2.0;
            double sum= 8*f(mid,4)+7*f(mid,3)+2*f(mid,2)+3*f(mid,1)+6;
            if(sum == m)
                break;
            else if(sum < m)
                l = mid;
            else
                r= mid;
        }
        printf("%.4f\n",mid);

    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/AC__GO/article/details/81178239