Turn the corner (HDU-2438)(三分查找)

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?

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

题意:给你两个路口的宽和车的长和宽,问你能不能把车开过这个路口。

思路:盗用大佬的图片

尽可能的贴着墙壁来过弯,所以能推导出-x先增大后减小,所以用到了三分的知识,具体看代码。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const double maxx=1e-6;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
using namespace std;
double x,y,l,w;
double sanfen(double jd)
{
    return (-x+l*sin(jd)+w/cos(jd))/tan(jd);
}
int main()
{
    while(cin>>x>>y>>l>>w)
    {
        double l=0,r=pi/2,mid,midr;
        while(maxx+l<r)
        {
            mid=l+(r-l)/3;
            midr=r-(r-l)/3;
            if(sanfen(mid)>sanfen(midr))
                r=midr;
            else
                l=mid;
        }
        if(sanfen(l)<y)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }
    return 0;
}
发布了204 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43846139/article/details/103669652
今日推荐