cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2020 · MediumTime arithmetic

Hour Hand Position

Autumn Tournament (2020)

Submission name: clock.cpp

Problem summary

A 12-hour clock initially shows h hours and m minutes. After dh hours and dm minutes, report either the new time (c = 1), or the hour hand’s position on the minute scale (c = 2). For an exact minute mark print @ followed by that mark; otherwise print the two adjacent marks in clockwise order.

Input

Five integers h, m, dh, dm, c.

Output

For c = 1 print hours (0–11) and minutes. For c = 2 print @ mark, or the preceding and following minute marks.

Constraints

0 ≤ h ≤ 11; 0 ≤ m ≤ 59; 0 ≤ dh ≤ 999; 0 ≤ dm ≤ 59; 1 ≤ c ≤ 2.

Worked example

Original statement, example 1.

Input

3 10 1 5 1

Output

4 15

Solution analysis

  1. Add all elapsed time in minutes and reduce modulo 720, the length of one 12-hour revolution. Dividing by 60 gives the new hour and the remainder gives the minute.
  2. The hour hand moves one minute-scale mark every 12 minutes. For normalized total t, its position is therefore t/12. Integer division gives the preceding mark and t % 12 detects an exact mark.
  3. Starting at 0:00 and waiting 3 hours 24 minutes gives t = 204. Since 204/12 = 17, the answer is @ 17. For t = 719 the adjacent marks are 59 and 0.

Why it works

Reduction modulo 720 preserves both hand positions. Since one full hour-hand turn covers 60 marks in 720 minutes, division by 12 gives its mark coordinate. The quotient and remainder identify exactly whether the position is a mark or lies between the reported consecutive marks.

Complexity: O(1) time and O(1) extra space.

Watch out. Use the hour hand, not the minute hand, for c = 2. Wrap the mark after 59 back to 0.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main(){
    int h,m,dh,dm,c;
    cin>>h>>m>>dh>>dm>>c;
    int total=(60*(h+dh)+m+dm)%720;
    if(c==1){
        cout<<total/60<<' '<<total%60<<'\n';
    }
    else if(total%12==0){
        cout<<"@ "<<total/12<<'\n';
    }
    else{
        // Consecutive marks are cyclic.
        cout<<total/12<<' '<<(total/12+1)%60<<'\n';
    }
}

Source: Arena Olimpiici ↗