Blog·Tanky WooABOUTRSS

HDOJ/HDU 2199 Can you solve this equation?

01 Feb 2011
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

题目传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=2199

分析:入门级二分搜索

直接使用cp技能,copy miyu的了~~~

代码:

#include 
#include 
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(100) < 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 = mid + 1e-6;
          }
          printf ( "%.4lf\n",( l + r ) / 2.0 ); 
    }
    return 0; 
}