Whole Minutes until Clock Hands Meet
National Olympiad — National round (2009 season)
Submission name: clock.cpp
Problem summary
Find the number of full minutes until the nearest overlap of analogue clock hands at exact time h:m. A current overlap is allowed.
Input
h m.
Output
The floor of the waiting time in minutes.
Constraints
0 ≤ h ≤11;0 ≤ m ≤59.
Worked example
Input and output for the worked example discussed below.
Input
3 18
Output
63
Solution analysis
- In half-degree units the hour-minus-minute angular gap is60h−11m. The minute hand gains11 half-degrees per minute, so no floating-point angles are needed.
- Reduce this gap modulo720, one full revolution. Divide the nonnegative gap by11 using integer division to count complete minutes.
- At3:18 the gap−18 becomes702 and floor(702/11)=63. At3:16 the wait4/11 minutes gives0. At0:00 the immediate overlap also gives0.
Why it works
Uniform relative motion covers the reduced gap at11 half-degrees per minute. The reduced nonnegative gap corresponds to the nearest meeting, and integer division gives its full-minute part.
Watch out. Do not round up or ignore hour-hand motion. Do not skip an overlap at the current instant.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
using namespace std;
int main(){
int hour;
int minute;
cin>>hour>>minute;
// Exact half-degree arithmetic includes motion of both hands.
int gap=(60*hour-11*minute+720)%720;
cout<<gap/11<<'\n';
}