HDUOJ2199 Can you solve this equation?

來源:互聯網
上載者:User

 

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

題目分析:

很明顯,這是一個2分搜尋的題目, 但是注意下題目的資料!! 1e10 的實數!! 而且精度是要求在 0.0001 . 所以就算是2分資料量依舊比較大,如果用
通常的遞迴方法嗎很遺憾 , RE了.............  沒辦法, 只能迴圈了.
下面的是遞迴 RE 的代碼 :

#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;}

AC代碼如下:

#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 ( "%.4lf/n",( l + r ) / 2.0 );<br /> }<br /> return 0;<br />} 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.