hdoj2438Turn the corner【三分法】



Turn the corner

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


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 4 10 6 14.5 4
 

Sample Output
      
      
yes no
 

题意:给定两条路的宽度和汽车的长宽判断汽车能否通过这两条路组成的拐角

解题思路:在保证车尾不会撞墙的情况下对角度进行三分求出车头所需要的最大宽度然后和道路判断即可


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define PI 3.141592653589793238
using namespace std;
double x,y,l,d;
double getH(double angle){
	double s=l*cos(angle)+d*sin(angle)-x;//汽车最右边的一个角和拐角的长度 
	double h=s*tan(angle)+d*cos(angle);//所占用的长度 
	return h;
}
int main()
{
	while(scanf("%lf%lf%lf%lf",&x,&y,&l,&d)!=EOF){
		double left=0,right=PI/2.0;
		double mid,mmid;//角度为汽车靠近墙的一边和水平线的夹角 
		int size=100;
		while(size--){//三分角度求出当H最大时的角度 
			mid=(left+right)/2.0;
			mmid=(mid+right)/2.0;
			double h1=getH(mid),h2=getH(mmid);
			if(h1>h2)
				right=mmid;
			else 
				left=mid;
		}
		if(getH(left)<=y)
			printf("yes\n");
		else 
			printf("no\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值