Can you solve this equation?
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2 100 -4
Sample Output
1.6152 No solution!
这题直接用二分做 但要注意精度
#include<bits/stdc++.h>
using namespace std;
double y;
int n;
double fun(double x){
return 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6;
}
int main(){
cin >> n;
while(n--){
cin >> y;
double l = 0;
double r = 100;
double mid;
if(fun(0) > y || fun(100) < y)
cout << "No solution!" << endl;//函数单调性 手推一下就出来了
else{
while(r - l > 0.000001){
mid = (l + r) / 2;
if(fun(mid) - y > 0)
r = mid + 0.0000001;
if(fun(mid) - y < 0)
l = mid - 0.0000001;
}
cout << fixed << setprecision(4) << (r + mid) / 2<< endl;//setprecision函数用法见我博客
}
}
}