hdu 2199 Can you solve this equation? 二分




1.精度问题 由于是double类型,r=mid 而不是r=mid-1

2.如果首位两端(f(0)和f(100))同号,证明解不在[1,100]区间内 这是我之所以TE的原因,没有预先判断

3.若在这个区间内,则一定可要求出解 所以binarysearch 返回m


#include <time.h>
#include <iomanip>
#include <cstdlib>
#include <stdio.h>
#define eps 1e-6
using namespace std;
double y;
double f(double x)
{
	return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6-y;
}
double binaryserch(double l, double r)
{
	double m;
	while((r-l)>=eps)
	{
		m=(l+r)/2;
		if(f(m)>0) r=m;
		else l=m;
	}
	return m;

}
int main()
{
	int num;

	cin>>num;
	while(num--)
	{
		cin>>y;
       if(f(0)*f(100)>0) cout<<"No solution!"<<endl;
		else printf("%.4lf\n",binaryserch(0,100));
	}
	return 0;
}

posted @ 2018-02-27 18:37  LandingGuys  阅读(95)  评论(0编辑  收藏  举报