HDOJ/HDU 2199 Can you solve this equation?

题目传送门:

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

分析:入门级二分搜索

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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(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; 
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

《HDOJ/HDU 2199 Can you solve this equation?》有219个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注