02:二分法求函数的零点

总时间限制: 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位。
样例输入

样例输出
不提供

#include<iostream>
#include<math.h>
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=1.5;
	double high=2.4;
	double mid=(low+high)/2;
	while(fabs(f(mid)-0)>=0.000001){
		if(f(mid)>0)
		low=mid;
		else
		high=mid;
		
		mid=(low+high)/2;
	}
	
	printf("%.6lf",mid);
	return 0;
}
发布了36 篇原创文章 · 获赞 0 · 访问量 324

猜你喜欢

转载自blog.csdn.net/weixin_44437496/article/details/104076202