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!
");
			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
", left);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/gavanwanggw/p/6718033.html