Problem Description
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>
Input
Every line has four real numbers, x, y, l and w.<br>Proceed to the end of file.<br>
Output
If he can go across the corner, print "yes". Print "no" otherwise.<br>
Sample Input
10 6 13.5 410 6 14.5 4
Sample Output
yesno
==========================================================================
题目大意:已知一直角弯入口处与出口处的路宽,及汽车的长与宽,判断该车是否能通过该直角弯。
作为三分求极值的题目,该题最关键的就是找出求解对应角度下所需要的出口路宽的函数表达式,自己最终也没想出来,感觉确实比较难。((h*cos(a)+w*sin(a)-x)*tan(a)+w*cos(a);)(h对应车的长度,a对应角度,w对应车的宽度)。
最终的ac代码:
#include<bits/stdc++.h>
using namespace std;
const double pi=acos(-1.0);//此方法得到较准确的π的值。
double x,y,h,w; //二分三分中一般用double或float数据类型。
double f(double a)
{
return (h*cos(a)+w*sin(a)-x)*tan(a)+w*cos(a); //该式子较难想,至今不明白。
}
int main()
{
while(cin>>x>>y>>h>>w)
{
double mid,midmid,l=0,r=pi/2;
while(fabs(l-r)>=1e-8)//二分三分的终止条件。
{
mid=(l+r)/2; //用三分求最值。
midmid=(mid+r)/2;
if(f(mid)>f(midmid))r=midmid; //怎么改变上下限,此处容易错。
else l=mid;
}
if(f(mid)<=y)cout<<"yes"<<endl; //若需要的路宽<=实际路宽,则yes。
else cout<<"no"<<endl;
}
return 0;
}
最后,关于该题,该题可先用于熟悉一下三分求极值的操作过程。