ACM-二分搜索之Can you solve this equation?——hdu2199

Can you solve this equation?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7507    Accepted Submission(s): 3490

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!

Author

Redow

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

这道题目刚开始以为是计算几何,

但是出在了搜索专题里,就知道用搜索来做。。。

然后分析是哪种搜索?

基础的BFS,DFS肯定不是,

连BFS都不是 双向BFS那就更不用想。

A*、IDA*  看着可能有点意思。。。

但是,别忘了还有二分搜索= =。

恩,这道题就是用二分来解。

控制一下精度,

对于精度判定(我的理解):

我觉得是,用printf直接输出一个double型,一般输出到6位小数,

我觉得就是默认保留的六位小数,于是精度设定1e-6.

但YM说是因为:

题目要求保留四位小数,一般会往后扩充两位小数来预防进位等情况,所以保留6位小数。

反正这道题是保证六位小数了,

还有要吐槽的一点,刚开始没仔细看测试数据,就看到1.615几,然后就交了,

AC后发现我的输入100会得出1.6151...题目样例是6.6152  o(╯□╰)o。。。。

这算不算水过= =。。。

再有一点,这题目测试数据中没有边界的判定。。。就是当y为6或者 8XXXXXX。。(将X等于100带入式子得到的Y)

/**************************************
***************************************
*        Author:Tree                  *
*From :http://blog.csdn.net/lttree    *
* Title : Can you solve this equation?*
*Source: hdu 2119                     *
* Hint  : 二分搜索                    *
***************************************
**************************************/

#include <stdio.h>
#define EPS 1e-6
double jdz(double a)
{
    return a<0?-a:a;
}
double calcu( double x )
{
    return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}
double binary_s( int y )
{
    double l,h,m;
    l=0,h=100;
    while( h-l>EPS )
    {
        m=(l+h)/2.0;
        if( calcu(m)>y )   h=m;
        else    l=m;
    }
    return (l+h)/2.0;
}
int main()
{
    int test,y;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d",&y);
        if( y<=6 || y>=calcu(100) )    printf("No solution!\n");
        else    printf("%.4lf\n",binary_s( y ) );
    }
    return 0;
}

ACM-二分搜索之Can you solve this equation?——hdu2199,码迷,mamicode.com

时间: 2024-10-12 07:41:35

ACM-二分搜索之Can you solve this equation?——hdu2199的相关文章

ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16281 Accepted Submission(s): 7206 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can

Can you solve this equation?---hdu2199(二分)

http://acm.hdu.edu.cn/showproblem.php?pid=2199 给出y的值求x: 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = Y x是0到100的实数所以要用二分的方法: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algori

hdu 2199:Can you solve this equation?(二分搜索)

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7493    Accepted Submission(s): 3484 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,

杭电 HDU ACM 2199 Can you solve this equation?

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11180    Accepted Submission(s): 5151 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 ==

hdu 2199 Can you solve this equation?(高精度二分)

http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13468    Accepted Submission(s): 6006 Problem Description Now,given the

hdu 2199 Can you solve this equation?

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 题目大意:找到满足8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y的x值,注意精确度问题. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 8 double fun(double s) 9 { 10 return

HDU 2199 Can you solve this equation?【二分查找】

解题思路:给出一个方程 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,求方程的解. 首先判断方程是否有解,因为该函数在实数范围内是连续的,所以只需使y的值满足f(0)<=y<=f(100),就一定能找到该方程的解,否则就无解. 然后是求解过程, 假设一个区间[a,b],mid=(a+b)/2,如果f(a)*f(b)<0,那么函数f(x)在区间[a,b]至少存在一个零点,如果f(a)<0,说明0点在其右侧,那么将a的值更新为当前mid的值,如果f(a)&g

Num 23 : HDOJ : 2199 Can you solve this equation? + HDOJ : 1969 Pie [ 二分法 ]

在处理计算数学上某函数零点的时候,我们通常是这样做的: 1. 先判断这个零点在某个单调区间 [ a,b ]上( 假设为递增区间 ): 2. 之后判断 f(x) 在 (a+b)/2 [ a,b中点处 ]是否为零: 3. 若中点处大于零,b=( a+b )/2 ;否则,同理,a=( a+b )/2 ; 4. 重复2.3.过程,直到找到零点,或满足精度: 如图( 图片来自百度 ): C语言基于这个原理,也有了自己的二分法: 二分法:     在某一特定的序列之中[ 通常是已经排过序的数列 ]寻找某一特

HDOJ-2199-Can you solve this equation?(二分查找)

Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11044    Accepted Submission(s): 5083 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 ==