您的位置:首页 > 其它

练习二 1005 Turn the corner

2016-04-23 20:53 274 查看
[align=left]题目:[/align]
[align=left]Problem Description[/align]
Mr. West bought a new car! So he is travelling around the city.<br><br>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.<br><br>Can Mr. West go across the corner?<br><img src=../../../data/images/2438-1.jpg><br>

[align=left]Input[/align]
Every line has four real numbers, x, y, l and w.<br>Proceed to the end of file.<br>

[align=left]Output[/align]
If he can go across the corner, print "yes". Print "no" otherwise.<br>

[align=left]Sample Input[/align]

10 6 13.5 4<br>10 6 14.5 4<br>

[align=left]Sample Output[/align]

yes<br>no<br>
题意:一辆长为l,宽为w的车要从宽为x的公路拐到宽为y的公路,问能不能成功。解题思路:画出符合题意的图,并根据图写出车辆能拐到y公路的最大宽度的方程h=f(a),由题意可知h为凸函数,所以我们只要求出h在(0.pi/2)的最值即可(三分法求出),且h=-(xtan(a)+lsin(a)+w/cos(a)),若h<y,则车可以通过,反之不可。感想:三分+几何计算问题。只要这个题的图画出来了一切的问题都会迎刃而解,不过虽然画出了图但是计算公式却写错了好几次,改了好几次才通过。代码:
#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const double PI=acos(-1.0);
double x,y,l,w;
double fun(double a)
{
return -((x-l*sin(a)-w/cos(a))/tan(a));
}
double find(double l, double r)
{
double mid,midmid;
while(r-l>1e-6)
{
mid=(l+r)/2;
midmid=(r+mid)/2;
if(fun(mid)>fun(midmid))
r=midmid;
else l=mid;
}
return l;
}
int main()
{
while(cin>>x>>y>>l>>w)
{
if(x<w||y<w||fun(find(0,PI/2))>y)
cout<<"no"<<endl;
else cout<<"yes"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: