zoukankan      html  css  js  c++  java
  • 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! ");
                continue;
            }//最大解和最小解判断有没有解
            else
            {
                while(true)
                {
                    x=(x1+x2)/2;
                    if(x2-x1<1e-6)//这里精度最小就得是1e-6
                    {
                        printf("%.4lf ",x);
                        break;
                    }
                    else
                    {
                        if(iteration(x)==y)
                        {
                            printf("%.4lf ",x);
                            break;
                        }
                        else if(iteration(x)
                            x1=x;
                        else if(iteration(x)>y)
                            x2=x;
                   }
                }
            }
         }
         return 0;
    }
  • 相关阅读:
    洛谷P1071 潜伏者
    2019BJFU 网站设计(孙俏-web前端开发)实验代码-181002222
    反思——P1307 数字反转
    洛谷P1067 多项式输出
    湖南大学第十五届程序设计竞赛(重现赛)
    2019河北省大学生程序设计竞赛(重现赛)
    2019BJFU C++实验习题(完结)
    配置android source 在ubuntu中编译环境
    Android屏幕保持唤醒状态
    Android richtext
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781646.html
Copyright © 2011-2022 走看看