Problem E
Problem Description
Mr. West bought a new car! Sohe is travelling around the city.<br><br>One day he comes to avertical corner. The street he is currently in has a width x, the street hewants to turn to has a width y. The car has a length l and a widthd.<br><br>Can Mr. West go across the corner?<br><imgsrc=../../../data/images/2438-1.jpg><br>
Input
Every line has four realnumbers, x, y, l and w.<br>Proceed to the end of file.<br>
Output
If he can go across thecorner, print "yes". Print "no" otherwise.<br>
Sample Input
10 6 13.5 4
10 6 14.5 4
Sample Output
yes
no
解题思路:
一开始一直试图用纯物理,数学,解出最优解并与车子的长宽作比较,测试数据过了,然而AC不了,,
下面是这段执着的bug,虽然我找不出bug具体在哪。。
bug.1-
#include<bits/stdc++.h>
using namespace std;
int main()
{
double x,y,l,d;
double l1,l2;
while(cin>>x>>y>>l>>d)
//cout<<"x="<<x<<" y="<<y<<" l="<<l<<" d"<<endl;
{l1=sqrt(y*y-d*d);
l2=l*1.0-l1;
//cout<<"l1="<<l1<<" l2="<<l2<<endl<<" sqrt(x*x-d*d)="<<sqrt(x*x-d*d)<<endl<<endl;
if(l2<=sqrt(x*x-d*d))
cout<<"yes\n";
else
cout<<"no\n";
}
return 0;
}(以两条路宽为半径分别做圆,当车沿有角一边走时,当其前方车头碰到小圆边界,则没有走出转角那部分剩余如果在大圆内,则可以通过,否则不能,因为以转角为中心,车身进行旋转,车子前后两对角线成圆弧状,,所以我依然还是不知道有什么毛病。。)
bug.2:万恶的“~”是个什么东西??有它就过,没它就超时。。。(看了题解疑惑的试了下,,就过了,,万脸懵x)
AC.1
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
double x,y,l,w;
double pi=acos(-1.0);
//double f(double an)
//{
// return l*cos(an)+(w-x*cos(an))/sin(an);
//}
double H(double sita,double l,double x,double w)//算出黄线的长度
{
return l*sin(sita)-x*tan(sita)+w/cos(sita);
}
int main()
{
double low,mid,mmid,high;
while(~scanf("%lf%lf%lf%lf",&x,&y,&l,&w))//~~~~~~~??????
{
low=0.000001;
high=pi/2;
while(high-low>0.00001)//精度达不到就wa
{
//double temp=(high-low)/3;
mid=(low+high)/2.0;
mmid=(mid+high)/2.0;
if(H(mid,l,x,w)>H(mmid,l,x,w))
high=mmid;
else low=mid;
}
if(H(mid,l,x,w)<=y)cout<<"yes\n";
else cout<<"no\n";
}
return 0;
}(类似于我的思路,,使这一边能够通过,看那一边是不是还能通过,,用角度等一步步推出公式)
本文解析了一道关于汽车能否顺利通过垂直拐角的算法题目,通过数学模型判断汽车是否可以安全转弯,提供了两种不同的解题思路及代码实现。
1507

被折叠的 条评论
为什么被折叠?



