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.
InputThe 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);
OutputFor 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
Now please try your lucky.
2 100 -4Sample Output
1.6152
No solution!
题意:就是找方程的解(二分法);
AC代码为:
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; double eps=1e-8; double judge(double x,double y) { return 8.0*x*x*x*x+7.0*x*x*x+2.0*x*x+3.0*x+6.0-y; } double bsearch(double y) { double left = 0, right = 100; double mid; while (right - left > eps) { mid = (left + right) / 2; if (judge(mid, y)>0) { right = mid; } else { left = mid; } } return mid; } int main() { int T; cin>>T; while(T--) { double num; cin>>num; if(num<6 || num>807020306) { printf("No solution! "); } else { double flag; flag=bsearch(num); printf("%.4lf ",flag); } } return 0; }