Television Sleep Timer
Autumn Tournament (2024)
Submission name: cat.cpp
Problem summary
A television timer is set at hour h and minute m to switch the television off N minutes later. Find the switch-off time on a 24-hour clock.
Input
h and m on the first line; N on the second.
Output
The switch-off time in hh:mm format.
Constraints
0 ≤ h ≤ 23; 0 ≤ m ≤ 59; 0 ≤ N ≤ 100000.
Worked example
The example is reproduced from the original tournament statement.
Input
23 10 70
Output
00:20
Solution analysis
- Convert h:m into 60h+m minutes after midnight. Adding N now handles all minute-to-hour carries automatically.
- Only the time of day is required. Reduce the sum modulo 1440, then divide by 60 for hours and take the remainder for minutes.
- At 23:10 there are 1390 elapsed minutes. Adding 70 gives 1460; modulo 1440 leaves 20, which is 00:20.
Why it works
Minute conversion preserves elapsed time. Every complete 1440-minute day leaves the same clock display, so the remainder identifies the switch-off time. Quotient and remainder by 60 recover its hour and minute.
Watch out. N can span many days. Print leading zeros, including at midnight.
Commented C++17 solution
An independently written complete program with standard headers.
// Key idea: Minute conversion preserves elapsed time. Every complete 1440-minute day leaves the same
// clock display, so the remainder identifies the switch-off time.
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int h,m,n;
cin>>h>>m>>n;
int time=(h*60+m+n)%1440;
cout<<setfill('0')<<setw(2)<<time/60<<':';
cout<<setw(2)<<time%60<<'\n';
}