NOIP学习之二分查找:123.二分法求函数的零点

一曲冷凌霜 提交于 2020-03-11 10:11:41

测试链接
总时间限制: 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<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
double cal(double x)
{
	double y;
	y=x*x*x*x*x-15*x*x*x*x+85*x*x*x-225*x*x+274*x-121;
	return y;	
}
int main()
{
	double left=1.5,right=2.40,mid;
	int can=mid;
	while(cal(left)>=cal(right))
	{

		mid=(left+right)/2.0;
		if(cal(mid)>0)
			left=mid;
		else if (cal(mid)<0)
			right=mid;
		else
		{
			printf("%.6lf",mid);
			return 0;
			}
	}
	printf("%.6lf",mid);
	return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!