Sample Output 1.6152 No solution!
簡單題意:
給出一個方程,求解方程,注意Y的范圍。
思路:
用二分法求解,
# include <iostream>
# include <cmath>
using namespace std;
double f(double x, double y)
{
double fx = 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6 - y;
return fx;
}
int main()
{
int t;
cin >> t;
while(t--)
{
double y;
cin >> y;
double begin = 0, end = 100, mid;
int i = 0;
while(1)
{
if(y < 6 || y > 8.0702e+8)
{
cout << "No solution!" << endl;
break;
}
mid = (begin + end) / 2;
if(fabs(f(mid, y)) <= 0.0001)
{
cout.precision(4);
cout << fixed << mid << endl;
break;
}
else if(f(mid, y) > 0)
{
end = mid;
}
else if(f(mid, y) < 0)
{
begin = mid;
}
//cout << mid << endl;
}
}
return 0;
}