您的位置:首页 > 其它

HDU 2199(二分求方程解)

2015-07-23 17:05 357 查看
关键是确定解的范围以及二分时的判断依据

这里解的范围已经确定,因为是递增函数,二分依据就是和Y的大小比较

#include"cstdio"
#include"cstring"
#include"algorithm"
#include"cmath"
#define MAXN 505
using namespace std;
double cal(double x)
{   double temp=8.0*pow(x,4)+7.0*pow(x,3)+2.0*pow(x,2)+3.0*x+6.0;
return temp;
}
int main()
{   int T;
scanf("%d",&T);
while(T--)
{   double ans,y;
scanf("%lf",&y);
double low=0.0,high=100.0,mid;
int ok=1;
if(y>cal(high)||y<cal(low)) ok=0;
while(ok&&fabs(high-low)>1e-8)
{   mid=low+(high-low)/2.0;
if(cal(mid)<y) low=mid;
else high=mid;
}
if(!ok) printf("No solution!\n");
else
printf("%.4lf\n",mid);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: