4142:二分法求函数的零点(二分查找)

 

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述

有函数:

f(x) = x5 - 15 * x4+ 85 * x3- 225 * x2+ 274 * x - 121

已知 f(1.5) > 0 , f(2.4) < 0 且方程 f(x) = 0 在区间 [1.5,2.4] 有且只有一个根,请用二分法求出该根。

输入
无。
输出
该方程在区间[1.5,2.4]中的根。要求四舍五入到小数点后6位。
样例输入
样例输出
1.849016


只输出结果,也可以通过。
#include <bits/stdc++.h>
using namespace std;

int fun(double x){
    double s=x*x*x*x*x-15*x*x*x*x+85*x*x*x-225*x*x+274*x-121;
    if(s>0)return 1;
    else return 0;
} 

int main() {
    double left=1.5,right=2.4;
    while(right-left>0.0000001){
        double mid=left+(right-left)/2;
        if(fun(mid))left=mid;
        else right=mid;
        
    }
    printf("%.6lf\n",left);
     
    return 0;
}



猜你喜欢

转载自www.cnblogs.com/aiqinger/p/12582367.html