zoukankan      html  css  js  c++  java
  • uva10668二分解方程

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1609

    题意:如图,给出弧长和弦长,求“?”处的距离h。

    思路:这里是二分半径r,要注意好精度,要不wa到死啊。特别要注意先判断一下n*c,如果结果太小则不能构成一个圆,就直接输出0好了。

    但是如果是二分h的话,精度就好控制很多。由于题目说细棒最多伸长为3L/2,所以h的范围是(0,L/2), r=(4*h*h+L*L)/(8*h).

    二分半径:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const double inf=0xffffffff;
    const double eps=1e-8;
    const int maxn=100;
    double l1,l2;
    double F(double r)
    {
        return 2*r*asin(l1/(2*r))-l2;
    }
    double binary()
    {
        double l=0.5*l1,r=inf,m;
        while(l+eps<r)//for(int i=0;i<150;i++)//
        {
            m=l+(r-l)/2;
            if(F(m)>eps) l=m;
            else r=m;
        }
        return m;
    }
    int main()
    {
       // freopen("in.cpp","r",stdin);
        double n,c,r;
        while(scanf("%lf%lf%lf",&l1,&n,&c))
        {
            if(l1<0 && n<0 && c<0) break;
            if(n*c<eps)
            {
                printf("0.000
    ");
                continue;
            }
            l2=(1+n*c)*l1;
            r=binary();
            double d=sqrt(r*r-l1*l1/4);
            printf("%.3lf
    ",r-d);
        }
        return 0;
    }
    View Code

    二分高度h:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const double inf=0xffffffff;
    const double eps=1e-8;
    const int maxn=100;
    double l1,l2;
    double F(double r)
    {
        return 2*r*asin(l1/(2*r))-l2;
    }
    double binary()
    {
        double left=0,right=l1/2,m;
        while(left+eps<right)//for(int i=0;i<150;i++)//
        {
            m=left+(right-left)/2;
            double r=(4*m*m+l1*l1)/(8*m);
            if(F(r)>eps) right=m;
            else left=m;
        }
        return m;
    }
    int main()
    {
    //    freopen("in.cpp","r",stdin);
        double n,c,r;
        while(scanf("%lf%lf%lf",&l1,&n,&c))
        {
            if(l1<0 && n<0 && c<0) break;
            l2=(1+n*c)*l1;
            double h=binary();
            printf("%.3lf
    ",h);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    python-登录小游戏
    easyclick 学习
    PYQT5 学习
    Pycharm之QT配置
    标贴打印机的基本使用
    开发遇到的问题及其解决
    Datatable 数据源
    JIRA操作之JQL
    类视图函数 VIEW
    前端基础
  • 原文地址:https://www.cnblogs.com/54zyq/p/3310719.html
Copyright © 2011-2022 走看看