zoukankan      html  css  js  c++  java
  • hdu 2199 二分法求单调函数方程的解

    首先,判断f(x)是单调递增的。 然后用, double 型的 二分模板

    题目来源:  

    http://acm.hdu.edu.cn/showproblem.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): 7258    Accepted Submission(s): 3362


    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 <algorithm>
    #include <stdlib.h>
    #include <stack>
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <algorithm>
    #include <stdlib.h>
    #include <vector>
    #include <set>
    #include <math.h>
    #include <cmath>
    #include <map>
    #include <stack>
    #include <queue>
    using namespace std ;
    
    typedef long long LL ;
    const double EPS=1e-8;
    
    double f(double x)
    {
        return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
    }
    double Bi_Search(double y)
    {
        double left,right,mid;
        left=0,right=100;
        while(right-left>EPS)
        {
        mid=(left+right)*0.5;
        if(f(mid)<y)
            left=mid;
        else right=mid ;
        }
        return (left+right)*0.5;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
        double y;
        cin>>y;
        if(y>=f(0)&&y<=f(100))
        printf("%.4lf
    ",Bi_Search(y));
        else cout<<"No solution!"<<endl;
        }
    return 0;
    }



  • 相关阅读:
    redis的五种常见数据类型的常用指令
    Linux常用的命令
    moco操作
    如何使用GoEasy实现PHP与Websocket实时通信
    浅谈websocket
    nginx 配置虚拟主机访问PHP文件 502错误的解决方法
    集群/分布式环境下5种session处理策略
    nginx 集群
    使用Nginx实现反向代理
    nginx的配置和基本使用命令
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3552516.html
Copyright © 2011-2022 走看看