zoukankan      html  css  js  c++  java
  • UVA 10341.Solve It-二分查找

    二分查找

    二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难。因此,折半查找方法适用于不经常变动而查找频繁的有序列表。首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
     
     
    这道题就是高中的一个定理,好像是零点定理。
    零点定理:
    设函数f(x)在闭区间[a,b]上连续,且f(a)与 f(b)异号(即f(a)× f(b)<0),那么在开区间(a,b)内至少有函数f(x)的一个零点,即至少有一点ξ(a<ξ<b)使f(ξ)=0。-----百度百科
    头尾相乘为<0,则有解,>0则无解。
     

     UVA10341.Solve It

     Solve the equation:
    p∗e−x + q∗sin(x) + r∗cos(x) + s∗tan(x) + t∗x2 + u = 0
    where 0 ≤ x ≤ 1.
    Input
    Input consists of multiple test cases and terminated by an EOF. Each test case consists of 6 integers in a single line: p, q, r, s, t and u (where 0 ≤ p, r ≤ 20 and −20 ≤ q,s,t ≤ 0). There will be maximum 2100 lines in the input file.
    Output
    For each set of input, there should be a line containing the value of x, correct up to 4 decimal places, or the string ‘No solution’, whichever is applicable.
    Sample Input
    0 0 0 0 -2 1

    1 0 0 0 -1 2

    1 -1 1 -1 -1 1
    Sample Output
    0.7071

    No solution

    0.7554

    代码如下:

    #include<stdio.h>
    #include<math.h>
    const double eps=1e-7;
    int p,q,r,s,t,u;
    double fun(double x){
        return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
    }
    int main(){
        while(~scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)){
            double maxx=1.0,minn=0.0,mid;
            if(fun(maxx)*fun(minn)>0){
                printf("No solution
    ");
                continue;
            }
            while(minn+eps<maxx){
                    mid=(maxx+minn)/2.0;
                if(fun(mid)<=0) maxx=mid;
                else minn=mid;
            }
           printf("%.4f
    ",mid);
        }
        return 0;
    }
     
     
  • 相关阅读:
    Docker(35)- docker inspect 命令详解
    Docker(34)- 如何修改 docker 容器的目录映射
    Docker(33)- 如何修改 docker 容器的端口映射
    Docker(32)- 如何修改 docker 容器的启动参数
    Docker(31)- docker import 命令详解
    Docker(30)- docker export 命令详解
    Docker(29)- docker top 命令详解
    Docker(28)- docker port 命令详解
    Docker
    Docker(27)- docker push 命令详解
  • 原文地址:https://www.cnblogs.com/ZERO-/p/6485986.html
Copyright © 2011-2022 走看看