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<stdio.h> #include<queue> #include<math.h> #include<string.h> #include<iostream> #include<stdlib.h> #include<algorithm> using namespace std; double f(double mid){ return pow(mid,5)-15*pow(mid,4)+85*pow(mid,3)-225*pow(mid,2)+274*mid-121; } int main(){ double base=1.5; double top=2.4; double mid; while(top-base>0.0000001){ mid=(base+top)/2; if(f(mid)>0){ base=mid; }else{ top=mid; } } printf("%.6f",mid); }