码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-25 08:46:34      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:二分   数学   

题意:给出一个数让你求出等于这个数的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;
}


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

标签:二分   数学   

原文地址:http://blog.csdn.net/shengweisong/article/details/38760251

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!