题目描述
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?
输入:
Every line has four real numbers, x, y, l and w.
Proceed to the end of file.
输出:
If he can go across the corner, print “yes”. Print “no” otherwise.
样例:
10 6 13.5 4
10 6 14.5 4
输出:
yes
no
题意
车转弯过程会不会被卡住~
思路
一开始想简单了,只判断45度角时的特殊情况,连样例都没过x
正常思路是车身贴边,建系求直线方程,通过函数找函数图像最高点是否满足条件。方程:y = tan(t)x+l*sin(t)+d/cos(t)。显然先增后减,最大值不一定在t=45°取到,验证了前面的想法是错的。然后标准三分找最大值,比较-x和y就可以啦
代码
#include <iostream>
#include <cmath>
using namespace std;
double x,y,l,w;
double finda(double t){
return (l*sin(t)+w/cos(t)-x)/tan(t);
}
int main(){
while(cin >> x >> y >> l >> w){
double left = 0,right = 3.1415926/2.0,ml,mr;
// cout << right << endl;
while(right-left>1e-6){
ml = (right-left)*1/3+left;
mr = (right-left)*2/3+left;
if(finda(ml)<=finda(mr)){
left = ml;
}
else right = mr;
}
if(finda(right)<=y)cout << "yes" << endl;
else cout <<"no" << endl;
}
return 0;
}