Can you solve this equation?
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 322 Accepted Submission (s): 148
Problem Description
Now,given the equation 8 * x ^ 4 + 7 * x ^ 3 + 2 * x ^ 2 + 3 * x + 6 = = Y,can you find its solution bet Ween 0 and 100;
Now try your lucky.
Input
The first line of the input contains an integer T (1 <= t <=) which means the number of test cases. Then T-lines follow, each line has a real number Y (Fabs (Y) <= 1e10);
Output
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!
Topic Analysis: Obviously, this is a 2-point search topic, but pay attention to the topic of data!! 1e10 of real numbers!! And the accuracy is required at 0.0001. So the amount of data is 2 is still relatively large, if used
What is the usual recursive method? Unfortunately, re has ..... There is no way, only the circulation.
The following is the code for recursive RE:
#include <iostream> #include <cmath>using namespaceStd;#define POW (x) ((x) * (x)) #define POW3 (x) (POW (x) * (x)) #define POW4 (x) (POW (x) * POW (x))DoubleY =0;BOOLDoueql (DoubleA,DoubleB ) {if (Fabs(A -B) <=1e-6 )returnTrue;returnFalse; }DoubleCal (DoubleN ) {return8.0 *POW4(N) +7 *POW3(N) +2 *POW(N) +3 *N +6 ; }DoubleBisearch (DoubleL,DoubleR ) {if (Doueql (L,R ) ) {if (Doueql (Y,Cal (L ) ) )returnL;return -1; }DoubleMid = (L +R ) /2.0;if (Doueql (Y,Cal (Mid ) ) )returnMid;else if (Cal (Mid) >Y )returnBisearch (L,Mid -0.0001 );Else returnBisearch (Mid +0.0001,R ); }intMain () {intT;scanf ("%d",&T ); while (T -- ) {scanf ("%lf",&Y );if (Cal(0) >=Y&&Cal(100) <=Y ) {Printf ("No solution!/n" );Continue; }DoubleRes =Bisearch (0.0,100.0 );if (Res == -1 )Printf ("No solution!/n" );ElsePrintf ("%.4lf/n",Res ); }return0; }
The AC code is as follows:
#include <iostream> #include <cmath> using namespace std; #define POW (x) ((x) * (x)) #define POW3 (x) (POW (x) * (x)) #define POW4 (x) (POW (x) * POW (x)) Double y = 0; Double cal (double N) {return 8.0 * POW4 (n) + 7 * POW3 (n) + 2 * POW (n) + 3 * n + 6;} int main () {int T; scanf ("%d ", &t); while (T--) {scanf ("%lf", &y), if (Cal (0) > y | | cal (+) < Y) {printf ("No solution!/n"); continue; } Double L = 0.0, r = 100.0,res = 0.0; while (R-l > 1e-6) {Double mid = (L + r)/2.0; res = cal (mid), if (res > y) r = mid-1e-6; else L = mi D + 1e-6; } printf ("%.4lf/n", (L + R)/2.0); } return 0; }