Turn the corner
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2785 Accepted Submission(s): 1102
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?
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.
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
题目大意:有一个转角,给你它的x,y和汽车的长l和宽d,问你它能否成功转弯。
思路分析:现象很常见,但我们很少去思考它能不能转弯,对于这种题目,一定要
将其转化为代数运算,建立合适的坐标系是比较常用的想法,
图像可以参考这个博客http://blog.csdn.net/u013761036/article/details/24588987
这样做是假设汽车贴着外边走,判断最宽的地方是否<Y,感觉假设贴着内边走也是能够做
出来的,至于为什么采用三分法,稍微感觉一下趋
势就可以,很显然最宽的时候是在转动的过程中,因此在【0,pi/2]之间二分即可
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
double x,y,l,w;
const double pi=acos(-1.0);
const double eps=1e-7;
double cal(double a)
{
double k=x-l*sin(a)-w/cos(a);
return -k/tan(a);
}
int main()
{
while(scanf("%lf%lf%lf%lf",&x,&y,&l,&w)!=EOF)
{
double l=0.0,r=pi/2;
if(x<=w|| y<=w) {printf("no ");continue;}
while(l+eps<=r)
{
double mid=(l+r)/2.0;
double mmid=(mid+r)/2.0;
if(cal(mid)<=cal(mmid)) l=mid;
else r=mmid;
}
if(cal(r)<y) printf("yes ");
else printf("no ");
}
return 0;
}
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
double x,y,l,w;
const double pi=acos(-1.0);
const double eps=1e-7;
double cal(double a)
{
double k=x-l*sin(a)-w/cos(a);
return -k/tan(a);
}
int main()
{
while(scanf("%lf%lf%lf%lf",&x,&y,&l,&w)!=EOF)
{
double l=0.0,r=pi/2;
if(x<=w|| y<=w) {printf("no ");continue;}
while(l+eps<=r)
{
double mid=(l+r)/2.0;
double mmid=(mid+r)/2.0;
if(cal(mid)<=cal(mmid)) l=mid;
else r=mmid;
}
if(cal(r)<y) printf("yes ");
else printf("no ");
}
return 0;
}
刚始那句特判必不可少,不太懂为什么,因为它弱wa了三发,
求高人指教。