HDU [ Turn the corner ]——三分+几何

Problem Description
Mr. West bought a new car! So he is travelling around the city.<br><br>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.<br><br>Can Mr. West go across the corner?<br><img src=../../../data/images/2438-1.jpg><br>
 

Input
Every line has four real numbers, x, y, l and w.<br>Proceed to the end of file.<br>
 

Output
If he can go across the corner, print "yes". Print "no" otherwise.<br>
 
Sample Input
 
 
10 6 13.5 410 6 14.5 4
 

Sample Output

 
 
yesno

==========================================================================

题目大意:已知一直角弯入口处与出口处的路宽,及汽车的长与宽,判断该车是否能通过该直角弯。


作为三分求极值的题目,该题最关键的就是找出求解对应角度下所需要的出口路宽的函数表达式,自己最终也没想出来,感觉确实比较难。((h*cos(a)+w*sin(a)-x)*tan(a)+w*cos(a);)(h对应车的长度,a对应角度,w对应车的宽度)。

最终的ac代码:

#include<bits/stdc++.h>
using namespace std;
const double pi=acos(-1.0);//此方法得到较准确的π的值。
double x,y,h,w;        //二分三分中一般用double或float数据类型。
double f(double a)
{
    return (h*cos(a)+w*sin(a)-x)*tan(a)+w*cos(a); //该式子较难想,至今不明白。      
}
int main()
{
   while(cin>>x>>y>>h>>w)
   {
       double mid,midmid,l=0,r=pi/2;
       while(fabs(l-r)>=1e-8)//二分三分的终止条件。
       {
           mid=(l+r)/2;      //用三分求最值。
           midmid=(mid+r)/2;
           if(f(mid)>f(midmid))r=midmid; //怎么改变上下限,此处容易错。
           else l=mid;
       }
       if(f(mid)<=y)cout<<"yes"<<endl;     //若需要的路宽<=实际路宽,则yes。
       else cout<<"no"<<endl;
   }
   return 0;
}

最后,关于该题,该题可先用于熟悉一下三分求极值的操作过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值