HDU 2438 Turn the corner 三角函数+三分查找法

来源:互联网 发布:知妈妈乐疯狂 编辑:程序博客网 时间:2024/06/07 02:16

题目地址:点击打开链接

Turn the corner

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3276    Accepted Submission(s): 1346


Problem Description
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 410 6 14.5 4
 

Sample Output
yesno

【题意】:车子要成功拐过这个弯道,最好的办法是使得车子的一条侧边靠墙走(然而现实中不存在);

【解析】:


根据图示,车子右侧沿着内拐角,左外侧沿着左侧墙壁。根据图中角度关系,即可得出关于h的函数,角度变化范围(0~90度)。

易得,函数是先增后减的。

用三分查找法找到函数h的最大值。最大值和y比较一下,会不会撞墙

【代码】:

#include<stdio.h>#include<math.h>double x,y,l,d;const double PI=3.14159;double h(double a){return d*cos(a)+d*sin(a)*sin(a)/cos(a)+l*cos(a)*tan(a)-x*tan(a);}int main(){while(~scanf("%lf%lf%lf%lf",&x,&y,&l,&d)){double m=0;double n=PI/2;double P,Q;while(fabs(m-n)>1e-6){P=m+(n-m)/3;Q=n-(n-m)/3;if(h(P)>h(Q))//左边大{n=Q;}else{m=P;}}if(h(m)<=y)puts("yes");elseputs("no");}return 0;}