1040: 方程求零点
1040: 方程求零点
时间限制: 1 Sec 内存限制: 128 MB提交: 276 解决: 108
[提交][状态][讨论版]
题目描述
设函数f(x)是在区间[a,b]内的连续函数,且f(a)f(b)<0, 根据Role定理,f(x)在区间内必存在零点。已知f(x)=x3-x-1, 输入区间端点的值,令精确度为eps=10-5, 判断是否存在零点,如果存在,输出该零点的近似值,否则输出No zero point.
输入
输出
样例输入
-1 0 0 1 1 1.5
样例输出
No zero point in area(-1.000000,0.000000) No zero point in area(0.000000,1.000000) The Zero Point is 1.32471 in area(1.000000,1.500000)
提示
来源
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double f(double n){
return n*n*n-n-1;
}
int main(){
double a,b,mid,left,right;
while(cin>>a>>b){
left=a;
right=b;
while(left<right){
mid=(left+right)/2;
if(fabs(f(mid))<0.000005){ //10的-5次方除以2
cout<<"The Zero Point is "<<setiosflags(ios::fixed)<<setprecision(5)<<mid<<" in area(";
cout<<setiosflags(ios::fixed)<<setprecision(5)<<a<<","<<b<<")"<<endl;
break;
}
if(f(left)*f(mid)<0){
right=mid;
}else{
left=mid;
}
}
if(left>=right){
cout<<"No zero point in area(";
cout<<setiosflags(ios::fixed)<<setprecision(5)<<a<<","<<b<<")"<<endl;
}
}
return 0;
}