Now please try your lucky.
2 100 -4Sample Output
1.6152 No solution!
思路:这题很明显就是利用二分的思想,先行判断函数是否为单调的,只有单调的才能够使用二分法,不断的逼近正确值,但要给一个精度值(要足够小),才能满足题意。一般的时候取1e-8,最后要求给出的小数点越多,这里的精度值就要越高。
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <iomanip>
using namespace std;
int check(double x,double y)
{
if (y - (8.0*pow(x, 4) + 7.0*pow(x, 3) + 2.0*pow(x, 2) + 3.0*x + 6)<0)
return 1;
else
return 0;
}
int main()
{
int T;
cin >> T;
while (T--)
{
double Y;
cin >> Y;
double left = 0, right = 100;
double mid;
if (Y < 6 || Y>807020306)
cout << "No solution!" << endl;
else
{
while (right - left > 1e-8)
{
mid = (left + right) / 2.0;
if (check(mid, Y))
right = mid;
else
left = mid;
}
cout << fixed << setprecision(4) << mid << endl;
}
}
return 0;
}