Loading...
Loading...
A puzzle app shows an analogue clock. Read the time as hours (1 to 12) and minutes, and print the smaller angle in degrees between the hour hand and the minute hand.
The minute hand moves 6° per minute. The hour hand moves 30° per hour and 0.5° per minute, so at 3:15 it is a little past the 3. Print the angle as a decimal number, like 90.0 or 7.5.
double.Example 1
Input
3 0
Output
90.0
The hands point at 3 and 12: 90 degrees.
Example 2
Input
3 15
Output
7.5
The minute hand is at 90°, the hour hand at 97.5°.
Try the problem first. Open one hint at a time.
Treat 12 as 0 for the hour hand: hours % 12.
Hour hand angle = 30 × h + 0.5 × m. Minute hand angle = 6 × m. Take the difference and make it positive.
If the difference is more than 180, the smaller angle is 360 minus it.
To avoid decimals, work with twice the angle (all whole numbers) and divide by 2.0 at the end.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor