• 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;
    }
  • 相关阅读:
    The 2019 ICPC Asia Shanghai Regional Contest H Tree Partition k、Color Graph
    回溯法、子集树、排列树、满m叉树
    顺时针打印矩阵
    单调递增的数字
    nodejs
    nodejs + express + mangodb 项目搭建
    nodejs + express 项目初始化
    星星评分功能(带小数点的那种,5颗星,10分制)
    easyui 增加删除toolbar 显示异常问题
    sql 外键级联,触发器防删
  • 原文地址:https://www.cnblogs.com/gpsx/p/5167638.html
Copyright © 2020-2023  润新知