HDOJ_ACM_Turn the corner

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
 

 Idea

To begin with, if d is smaller than x or y, then it's sure that the car couldn't pass the conner. Besides, we can considerate the l, which can be composed by a.

l = [(x / sin(a) + y / cos(a)) * cos(a) - d / sin(a)] / cos(a)

simplify the equality, we can get

l = x / sin(a) + y / cos(a) - d / sin (a) / cos(a)

then I can get l' and l'', but I can't find the any regulation about those.

what's a pity!

so I just use trichotomy to get similarity of l. I know it's luck.

Code

 1 #include <stdio.h>
 2 #include <math.h>
 3 double x, y, l, d;
 4 double f(double a)
 5 {
 6     return x / sin(a) + y / cos(a) - d / sin(a) / cos(a);
 7 }
 8 void main()
 9 {
10     double low, high, mid1, mid2;
11     int i;
12     while (scanf("%lf %lf %lf %lf", &x, &y, &l, &d) != EOF)
13     {
14         //there are two situation
15         if (d >= x || d >= y)
16         {
17             puts("no");
18         }
19         else
20         {
21             high = acos(-1.0) / 2;
22             low = 0;
23             while (fabs(high - low) >= 1e-7)
24             {
25                 mid1 = (high + low) / 2;
26                 mid2 = (mid1 + high) / 2;
27                 //printf("%f, %f, %f, %f\n",
28                 //        mid1, mid2, f(mid1), f(mid2));
29                 if (f(mid1) < f(mid2))
30                     high = mid2;
31                 else
32                     low = mid1;
33             }
34             //printf("high = %f, low = %f\n", high, low);
35             if (l < f(low))
36                 puts("yes");
37             else
38                 puts("no");
39         }
40     }
41 }
 

Key Points

Pay more attention to the algorithm.

 
原文地址:https://www.cnblogs.com/chuanlong/p/2961785.html