现在的位置: 首页 > 综合 > 正文

HDU-2199 二分搜索解方程

2013年02月19日 ⁄ 综合 ⁄ 共 511字 ⁄ 字号 评论关闭

题目连接

分析:

  该方程左边的明显就是在[0,100]区间单调递增的,这就适应了二分查找了。直接水过之。

#include<stdio.h>
#include<math.h>
#define eps 1e-10 
int sign(double x)
{
	return x<-eps?-1: x>eps;
}
double count(double x)
{
	return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
}
double bsearch(double y)
{
	double i=0,j=100;
	while(sign(j-i)>=0){
		double mid=(i+j)/2;
		double s=count(mid);
		if(sign(s-y)>0)
			j=mid-eps;
		else 
			i=mid+eps;	 
	}
	return i;
}

int main()
{
	int t;
	double y,x;
	scanf("%d",&t);
	while(t--){
		scanf("%lf",&y);
		if(count(0)<=y&&y<=count(100))
		     printf("%.4lf\n",bsearch(y));
		else  
		     puts("No solution!");		 
	}
	return 0;
}

抱歉!评论已关闭.