openjudge8206_二分法求函数的零点

openjudge8206_二分法求函数的零点

时空限制    1000ms/64MB

描述

有函数:

f(x) = x^5 - 15 * x^4+ 85 * x^3- 225 * x^2+ 274 * x - 121

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

输入

无。

输出

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

样例输入

样例输出

不提供

代码

#include<iostream>
#include<cstdio>
using namespace std;

double f(double x){
	return x*x*x*x*x-15*x*x*x*x+85*x*x*x-225*x*x+274*x-121;
}

int main(){
	double low,mid,high;
	low=1.5; high=2.4;
	while (low<=high){
		mid=(low+high)/2;
		if (f(mid)>1e-9) low=mid+1e-7;
		else high=mid-1e-7;
	}
	printf("%.6lf\n",low);
}

猜你喜欢

转载自blog.csdn.net/WDAJSNHC/article/details/81428685
今日推荐