您的位置:首页 > 其它

二分法求解方程根

2013-12-05 00:00 393 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2199

两分法求解方程根

code:

#include <iostream>
#include <cstdlib>
#define EPS 1e-20		//10的负20次方
using namespace std;
const double d=1e-8;
bool resolve(double x,double y)
{
if(y-(8.0*x*x*x*x+7.0*x*x*x+2.0*x*x+3.0*x+6.0)>=EPS)
{
return true;
}
return false;
}
int main(int argc, char *argv[])
{

int t;
scanf("%d",&t);
while(t--)
{
double l,r,m,y;
scanf("%lf",&y);
if(y<6||y>807020306)	//x范围是0-100
{
cout<<"No solution!"<<endl;
}else
{
l=0;
r=100;
while(r-l>d)
{
m=(l+r)/2.0;
if(resolve(m,y)) l=m;
else r=m;
}
printf("%.4lf\n",m);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: