• 二分法解具有单调性的方程


    解方程的手段有很多,如二分法、牛顿迭代法等等,本次介绍的是采用二分法,在使用二分法解方程时,有一点应该特别注意,就是那种“具有单调性”的函数才可以,否则是会有问题的

    ** HDU 2199**
    Problem Description
    Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 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<math.h>
    #include<iomanip>
    using namespace std;
    double funct(double x);
    double solve(double s,double e,double y);
    int main()
    {
        int t;
        cin>>t;
        double ant;
        double y;
        while(t--){
            cin>>y;
            ant=solve(0,100,y);
            if(ant!=-1) cout<<fixed<<setprecision(4)<<ant<<endl;
            else cout<<"No solution!"<<endl;
    
        }
    
        return 0;
    }
    double funct(double x){
        return 8*pow(x,4)+7*pow(x,3)+2*x*x+3*x+6;
    }
    double solve(double s,double e,double y){
    double mid;
    while(s<=e&&s>=0&&e<=100){
        mid=(s+e)/2;
        if(funct(0)>y||funct(100)<y) return -1;//由于函数单调递增,只需判断边界解就可确定目标范围内是否有解
        if(fabs(funct(mid)-y)<=0.0001) return mid;//解的误差符合要求,返回解
        if(funct(mid)<y) s=mid;
        else if(funct(mid)>y) e=mid;
    }
    return -1;//-1标记无解的情况
    }
    
    

    相似的一题:HDU 2899 strange function


    Problem Description
    Now, here is a fuction:
    F(x) = 6 * x7+8*x6+7x3+5*x2-yx (0 <= x <=100)
    Can you find the minimum value when x is between 0 and 100.

    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 only one real numbers Y.(0 < Y <1e10)

    Output
    Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

    Sample Input

    2
    100
    200

    Sample Output

    -74.4291
    -178.8534

    #include <iostream>
    #include<math.h>
    #include<iomanip>
    using namespace std;
    double funct(double x,double y);
    double ff(double x,double y);
    double solve(double s,double e,double y);
    int main()
    {
        int t;
        cin>>t;
        double ant;
        double y;
        while(t--){
            cin>>y;
            ant=solve(0,100,y);
          cout<<fixed<<setprecision(4)<<funct(ant,y)<<endl;
    
        }
    
        return 0;
    }
    double funct(double x,double y){
        return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-x*y;
    }
    double ff(double x,double y){
        return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x-y;
    }
    double solve(double s,double e,double y){
    double mid;
    while(s<=e&&s>=0&&e<=100){
        mid=(s+e)/2;
        if(ff(0,y)>0||ff(100,y)<0) return 0;
        if(fabs(ff(mid,y))<=0.0001) return mid;
        if(ff(mid,y)<0) s=mid;
        else if(ff(mid,y)>0) e=mid;
    }
    }
    
    

    该题的代码是我直接拿上一题的代码稍微修改了一下,思路其实都是一样的。应该说还是比较简单的一类题目

  • 相关阅读:
    xhtml中隐藏滚动条
    一个题目包括70种网页制作常用技巧 (转)
    Oracle 数据类型映射 (ADO.NET)
    oracle数据库 数据类型
    VisualSVN Server的配置和使用方法
    oracle 权限管理
    项目管理实践【三】每日构建【Daily Build Using CruiseControl.NET and MSBuild】
    showModalDialog和showModelessDialog使用心得
    Bug跟踪管理【Bug Trace and Management】
    开发WAP站点之使用PC电脑浏览器访问WAP手机站点 (转)
  • 原文地址:https://www.cnblogs.com/gao-hongxiang/p/12342428.html
Copyright © 2020-2023  润新知