zoukankan      html  css  js  c++  java
  • (二分搜索)Can you solve this equation? -- hdu -- 2199

    链接:

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

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 13475    Accepted Submission(s): 6010


    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!

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <stdlib.h>
    #include <math.h>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    const int oo = 0x3f3f3f3f;
    const int N = 222222;
    
    
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            int flag=0;
            double y, l=0, r=100, x;
            scanf("%lf", &y);
    
            double m = 8*100*100*100*100 + 21*100*100*100 + 4*100*100 + 3*100 + 6;
    
            if(fabs(y-m)<0.0001)
            {
                printf("100.0000
    ");
                continue;
            }
    
            while(l<r)
            {
                x = (l+r)/2;
                m = 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6;
    
                if(fabs(m-y)<0.0001)
                {
                    flag = 1;
                    break;
                }
                else if(m>y)
                    r = x;
                else if(m<y)
                    l = x;
            }
    
            if(flag)
            printf("%.4f
    ", x);
            else
            printf("No solution!
    ");
        }
        return 0;
    }
    勿忘初心
  • 相关阅读:
    基于HTML5的多张图片上传
    如何限制textarea文本框的输入字数
    页面第一次加载实现图片淡入方式加载
    ajax实现的无刷新分页代码实例
    26个Jquery使用小技巧
    Windows下搭建PHP开发环境
    Jquery插件之ajaxForm ajaxSubmit的理解用法
    Java XML解析器
    JS截取字符串
    在Eclipse中配置tomcat
  • 原文地址:https://www.cnblogs.com/YY56/p/4783093.html
Copyright © 2011-2022 走看看