hdoj 2199 Can you solve this equation? 【二分枚举】

题意:给出一个数让你求出等于这个数的x

策略:如题。因为整个式子是单调递增的,所以可以用二分。

要注意到精度.

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#define eps 1e-10
#define f(x)  8*pow(x, 4) + 7*pow(x, 3) + 2*pow(x, 2) + 3*x
int main()
{
	int t;
	double n;
	scanf("%d", &t);
	while(t --){
		scanf("%lf", &n);
		n-=6;
		double max = f(100);
		if(n<0||n>max){
			printf("No solution!\n");
			continue;
		}
		double left, right, mid;
		left = 0; right = 100;
		while(right-left > 1e-7){  //精度要小于1e-7,
			mid = (left+right)/2;
			double temp = f(mid);
			if(temp < n) left = mid+1e-7;
			else right = mid;
		}
		printf("%0.4lf\n", left);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值