HDU 2199 Can you solve this equation? (二分法解方程)

时间:2021-12-27 22:11:50

思路:

注意这个方程在给定的范围内是单调递增的。

坑点:

注意double类型不能直接判断 == 判断a和b相不相等要借助eps(精度)写成 if( (a - b) < eps )

#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#define eps 0.0000000001
typedef long long int lli;
using namespace std;

double f(double x,double y){
return 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6 - y;
}

double func(double y){
double l = 0,r = 100;
double mid = (l+r) / 2.0;
while(r-l > eps){
mid = (r+l) / 2.0;
double temp = f(mid,y);
if( temp > eps){
r = mid;
}
else{
l = mid;
}
}
return r;
}

int main(){
int t;
cin>>t;
int y;
while(t--){
scanf("%d",&y);
if(f(0,y) * f(100,y) > eps){
printf("No solution!\n");
}
else{
printf("%.4f\n",func(y));
}
}
}