1005 Turn the corner
题意:长L宽W的汽车,判断它是否能够从宽度为X的街道进入宽度为Y的街道。
思路:
在通过分析后可以得到,随着汽车由X进入Y,图中H的高度先增大后减小,而解几何图形后可得s = l * cos(θ) + w * sin(θ) - x;
h =s * tan(θ) + w * cos(θ);在题目中,因为高度先增大后减小,高度的增长趋势并不确定,因此需要用三分法。
感想:一开始看到题,有些懵,后来知道了其中的关系式后,再利用三分解答,倒也并没有什么困难了!
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
double pi=acos(-1.0);
double x,y,l,w,h,s;
double car(double a){
s=l*cos(a)+w*sin(a)-x;
h=s*tan(a)+w*cos(a);
return h;
}
int main(){
double f,r,mid,midmid;
while(scanf("%lf%lf%lf%lf",&x,&y,&l,&w)!=EOF){
f=0.0;
r=pi/2;
while(fabs(r-f)>1e-10){
mid=(f+r)/2;
midmid=(mid+r)/2;
if(car(mid)>=car(midmid)) r=midmid;
else f=mid;
}
if(car(mid)<=y) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}