您的位置:首页 > 其它

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

2017-04-16 10:05 405 查看
题意:给出一个数让你求出等于这个数的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: