JohnnysCannon
TC China 08 - 1E · 2008-11-23 · by mateuszek
Problem Statement
Johnny has recently constructed a cannon, and he wants to a hit a ground target that is distance units away. The cannon shoots bullets at velocity units per second. When a bullet is in the air, its flight follows the standard laws of physics. This means that if he shoots a bullet at angle alpha from the ground, it will travel a distance of
( 2 * velocity^2 * sin(alpha) * cos(alpha) ) / g ,
where g is the acceleration of gravity on Earth. In this problem, we will use 10 as the value of g.
The cannon can only be set at the angles given in the
Notes
- The returned value must be accurate to within a relative or absolute value of 1e-9.
Constraints
- velocity will be between 1 and 1000, inclusive.
- distance will be between 0 and 100000, inclusive.
- angles will contain between 1 and 50 elements, inclusive.
- Each element of angles will be between 0 and 90, inclusive.
5
40
{ 0, 45, 90 }
Returns: 37.5
Here we can choose 0, 45 or 90 degrees. The first and the last options are not very clever as we will shoot ourselves rather than hitting any target. So, the best possibility is 45 degrees.
10
5
{ 23, 76, 33, 12, 45 }
Returns: 0.30528437214108894
Here are the distances the bullet will travel using the given angles: 23 degrees: 7.193... 76 degrees: 4.694... 33 degrees: 9.135... 12 degrees: 4.067... 45 degrees: 10.0 We will be closest to hitting the target if we choose 76 degrees.
100
15
{ 4, 55, 22, 13, 7, 88, 90 }
Returns: 14.999999999999877
120
20367
{ 4, 55, 22, 13, 7, 88, 90 }
Returns: 19013.842626068294
10
5
{90,89,1,2}
Returns: 4.302435262558747
Submissions are judged against all 87 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class JohnnysCannon with a public method double getDistance(int velocity, int distance, vector<int> angles) · 87 test cases · 2 s / 256 MB per case