• Turn the corner



    Problem Description
    Mr. West bought a new car! So he is travelling around the city.

    One day he comes to a vertical corner. The street he is currently in has a width x, the street he wants to turn to has a width y. The car has a length l and a width d.

    Can Mr. West go across the corner?
    Turn <wbr>the <wbr>corner

    Input
    Every line has four real numbers, x, y, l and w.
    Proceed to the end of file.

    Output
    If he can go across the corner, print "yes". Print "no" otherwise.

    Sample Input
    10 6 13.5 4
    10 6 14.5 4

    Sample Output
    yes
    no
    题意:西先生开始出去,遇到一个弯,让你求能不能拐过去;
    解题思路:假设过弯时l与底边夹角为s,用三分求最理想过弯角度(用过弯过程中与转弯之后的宽度y相比较),找到最合适的角度,如果最合适的角度,转弯的宽度还是比y大就肯定转不过去,反之就能转过去;
    感悟:寻找过弯条件是最难的,找到之后就简单多了,记录第二道三分题;
    代码(G++ 0ms):
    #include
    #include
    #include
    #define pi 3.141592653589793238
    using namespace std;

    double x,y,l,d;
    double cal(double s)//进行三分的条件
    {
        return l*cos(s)+(d-x*cos(s))/(sin(s));
        //l*cos(s)+(d-x*cos(s))/(sin(s))就是过弯过程中在y方向上的宽
    }

    int main()
    {
        //freopen("in.txt", "r", stdin);
        double left,right,mid,midmid;
        while(scanf("%lf%lf%lf%lf",&x,&y,&l,&d)!=EOF)
        {
            //printf("x=%.1f y=%.1f l=%.1f d=%.1f ",x,y,l,d);
            if(d>x||d>y)
            {
                printf("no ");
                continue;
            }
            left=0;
            right=pi/2;//最大只能是pi/2,不可能倒着转弯吧
            //printf("left=%.1f right=%.1f ",left,right);
            while (right-left>1e-10)
            {
                mid=(left+right)/2;
                midmid=(mid+right)/2;
                //printf("mid=%.4f mid mid=%.4f ",mid,midmid);
                if (cal(mid)>=cal(midmid))//过弯过程中在y方向上的宽,与y比较
                    right=midmid;
                else left=mid;
            }
            //printf("right=%.1f ",right);
            if(cal(right)>y)//如果最大角度过弯时还是比y宽就肯定过不去
                printf("no ");
            else
                printf("yes ");
        }
        return 0;
    }
  • 相关阅读:
    Oracle在Linux下使用异步IO(aio)配置
    慢慢聊Linux AIO
    ssh批量登录并执行命令(python实现)
    Reverse Linked List II
    Reverse Linked List
    Excel Sheet Column Title
    Summary Ranges
    面试题1:实现大数加减乘除四则运算
    Multiply Strings
    Evaluate Reverse Polish Notation
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781631.html
Copyright © 2020-2023  润新知