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.
输入
输入包括若干行,表示该区间端点
输出
每行对应每个区间的计算结果,如果根存在,保留5位小数。对于有根区间,如果|f(x*)|<=eps,则x*为零点的近似值。
样例输入
-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.00005){
cout<<"The Zero Point is "<<setiosflags(ios::fixed)<<setprecision(5)<<mid<<" in area(";
cout<<setiosflags(ios::fixed)<<setprecision(6)<<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(6)<<a<<","<<b<<")"<<endl;
}
}
return 0;
}