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

    题目链接

    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!
     
    题解:二分搜索,精度见代码。一直WA,找不到错,最后发现是少了一个感叹号!坑啊。
    判断无解利用分析这个函数的导函数在[0,100]上恒大于0,所以这个函数在[0,100]上单调递增的性质来判断。
     
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    using namespace std;
    #define LOCAL
    int y;
    double fun(double x)
    {
        return 8*pow(x,4.0)+7*pow(x,3.0)+2*pow(x,2.0)+3*x+6;
    }
    void solve()
    {
        if(fun(0)>y||fun(100)<y)
        {
            printf("No solution!
    ");
            return;
        }
        double a=0,b=100,ans,m;
        while(b-a>1e-6)
        {
            m=(a+b)/2;
            ans=fun(m);
            if(ans>y)b=m-1e-7;
            else a=m+1e-7;
        }
        m=(a+b)/2.0;
        printf("%.4lf
    ",m);
        return;
    }
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif // LOCAL
        //Start
        int N;
        cin>>N;
        while(N--)
        {
            cin>>y;
            solve();
        }
        return 0;
    }
  • 相关阅读:
    Struts2-1.配置&与第一个应用
    1.rs.first()、rs.last()、rs.next()、rs.getRow()
    网页跳转
    js---DOM元素节点
    4、BufferedIn(out)putStream--->字节输入/输出流的缓冲区类(高效类:高效率读写)
    3、FileInputStream--->类文件输入流(读取文件数据)
    2、FileOutputStream--->文件输出流(向文件写入数据)
    1、IO输入&输出流 简介
    OutOfMemoryError系列
    Spark调优,性能优化
  • 原文地址:https://www.cnblogs.com/gpsx/p/5167638.html
Copyright © 2011-2022 走看看