cppAcademy
FROM LEARNING TO COMPETING

Real problems.
Your next challenge.

Explore the Group C Olympiad archive.
Choose a topic. Find a challenge. Build your solution.

213 tasks across the Municipal, Regional, and National rounds (2009–2026), plus 4 previously featured problems. 146 tasks have independently written English summaries, analyses, and C++17 solutions; the remaining entries link to Arena. Topic tags are from Arena or our reviewed explanations. Difficulty labels are learning estimates, not official ratings.
GROUP C2011 · MediumAngles

Angle Between Clock Hands

National Olympiad — National round

Submission name: clock.cpp

Problem summary

Find the smaller angle between continuously moving hour and minute hands at the specified time.

Input

A time in HH:MM format.

Output

Angle in degrees with exactly three digits after the decimal point.

Constraints

01 ≤ HH ≤ 12; 00 ≤ MM ≤ 59.

Worked example

An independently prepared example for this explanation.

Input

03:30

Output

75.000

Solution analysis

  1. The minute hand is at 6M degrees; the hour hand is at 30(H mod 12)+M/2 degrees.
  2. Take their absolute difference and the smaller of it and 360 minus it.

Why it works

Both positions follow constant angular velocities; complementary arcs determine the two possible angles.

Complexity: O(1) time and space.

Watch out. The hour hand moves between hour marks.

Commented C++17 solution

An independently written complete program with standard headers.

// Key idea: Both positions follow constant angular velocities; complementary arcs determine the two
// possible angles.
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstdlib>
using namespace std;
int main(){
    int h,m;
    char colon;
    cin>>h>>colon>>m;
    double d=abs(30.0*(h%12)+0.5*m-6*m);
    cout<<fixed<<setprecision(3)<<min(d,360-d)<<'\n';
}

Source: Arena Olimpiici ↗