Can you solve this equation?

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!
题意:利用二分法求解;
解题思路:用最简单的二分就能过,但是!!!注意精度最少1e-6;
感悟:终于知道学长说的,二分最蛋疼的就是控制精度了;
代码:
#include
#include
using namespace std;
double iteration(double x)
 
    double fx;
    fx=8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
    return fx;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int n;
    double y,x1,x2,x;
    scanf("%d",&n);
    for(int i=0;i
    {
        scanf("%lf",&y);
        x1=-1;
        x2=101;
        if(y<6||y>807020306)
        {
            printf("No solution!\n");
            continue;
        }//最大解和最小解判断有没有解
        else
        {
            while(true)
            {
                x=(x1+x2)/2;
                if(x2-x1<1e-6)//这里精度最小就得是1e-6
                {
                    printf("%.4lf\n",x);
                    break;
                }
                else
                {
                    if(iteration(x)==y)
                    {
                        printf("%.4lf\n",x);
                        break;
                    }
                    else if(iteration(x)
                        x1=x;
                    else if(iteration(x)>y)
                        x2=x;
               }
            }
        }
     }
     return 0;
}
posted @ 2016-03-22 21:55  勿忘初心0924  阅读(97)  评论(0编辑  收藏  举报