HDU-2199 二分法

二分法求方程的根

/*
 * hdu-2199 can you solve the equatino
 * mike-w
 * 2012-4-17
 *
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define EPS (1e-6)
#define nDISP_X1_X2

const int co[10]={8,7,2,3,6};

double foo(double x)
{
	double x0=co[0];
	int i;
	for(i=0;i<4;i++)
		x0=x*x0+co[i+1];
	return x0;
}

double solve(double Y)
{
	double x1=0.0, x2=100.0, mid;
	double y1=foo(x1), y2=foo(x2);
	while(fabs(y1-y2)>EPS)
	{
#ifdef DISP_X1_X2
		printf("x1,x2 = %lf,%lf\n",x1,x2);
		printf("y1,y2 = %lf,%lf\n",y1,y2);
#endif
		mid=(x1+x2)/2;
		if(foo(mid)>Y)
			y2=foo(x2=mid);
		else
			y1=foo(x1=mid);
	}
	return x1;
}

int main(void)
{
	int T;
	double min_v=(double)foo(0);
	double max_v=(double)foo(100);
	double Y;
	scanf("%d",&T);
	while(T-->0)
	{
		scanf("%lf",&Y);
		if(Y>max_v || Y<min_v)
			printf("No solution!\n");
		else
			printf("%.4lf\n",solve(Y));
	}
	return 0;
}

		



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值