补题之路:HDU2438 Turn the corner

题目描述

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值