Hduoj2199 can you solve this equation?

Source: Internet
Author: User
Tags cmath

 

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 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 shoshould 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!

Question Analysis:

Obviously, this is a 2-point search question, but pay attention to the data of the next question !! The real number of 1e10 !! And the precision is 0.0001, so even if the data size is 2 points
Is it a pity that the usual recursive method is... re... no way, it can only loop.
The following is the recursive re code:

#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;bool douEql ( double a,double b ){if ( fabs( a - b ) <= 1e-6  )return  true;return false;}double cal ( double n ){return 8.0 * POW4(n) + 7 * POW3(n) + 2 * POW(n) + 3 * n + 6 ;}double biSearch ( double l, double r ){if ( douEql ( l,r ) ){if ( douEql ( y, cal ( l ) ) )return l;return -1;}double mid = ( l + r ) / 2.0;if ( douEql ( y, cal ( mid ) ) )return mid;else if ( cal ( mid ) > y )return biSearch ( l,mid - 0.0001 );elsereturn biSearch ( mid + 0.0001, r );}int main (){int T;scanf ( "%d",&T );while ( T -- ){scanf ( "%lf",&y );if ( cal(0) >= y && cal(100) <= y ){printf ( "No solution!/n" );continue;}double res = biSearch ( 0.0, 100.0 );if ( res == -1 )printf ( "No solution!/n" );elseprintf ( "%.4lf/n",res );}return 0;}

The AC code is as follows:

# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> # define POW (x) (X) * (x) <br/> # define pow3 (x) (POW (x) * (x) <br/> # define pow4 (x) (POW (X) * POW (x) <br/> double y = 0; <br/> double CAL (double N) <br/> {<br/> return 8.0 * pow4 (n) + 7 * pow3 (n) + 2 * POW (n) + 3 * n + 6; <br/>}< br/> int main () <br/>{< br/> int t; <br/> scanf ("% d", & T ); <br/> while (t --) <br/> {<Br/> scanf ("% lf", & Y); <br/> If (CAL (0)> Y | CAL (100) <Y) <br/>{< br/> printf ("no solution! /N "); <br/> continue; <br/>}< br/> double L = 0.0, r = 100.0, Res = 0.0; <br/> while (R-l> 1e-6) <br/>{< br/> double mid = (L + r)/2.0; <br/> res = CAL (MID); <br/> If (RES> Y) <br/> r = mid-1e-6; <br/> else <br/> L = Mid + 1e-6; <br/>}< br/> printf ("%. 4 LF/N ", (L + r)/2.0); <br/>}< br/> return 0; <br/>} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.