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 E2010 · EasyArithmetic & formulas

Traffic Light Cycle

National Olympiad — Municipal round (2010 season)

Submission name: traffic.cpp

Problem summary

A traffic light starts green at 08:00:00 and repeats green for 60 seconds, yellow for 4, red for 40, and yellow for 4. Determine its color at a given time of day, including times before 08:00.

Input

Hour X, minute Y, second Z.

Output

premini for green, izchakai for yellow, or spri for red.

Constraints

0 ≤ X < 24; 0 ≤ Y, Z < 60.

Worked example

An independently prepared example for this explanation.

Input

8 1 0

Output

izchakai

Solution analysis

  1. One full cycle lasts 60 + 4 + 40 + 4 = 108 seconds. Convert the given time to seconds and subtract 8 × 3600.
  2. Normalize the remainder into [0, 107], including negative offsets. The green interval is [0, 59], first yellow [60, 63], red [64, 103], and second yellow [104, 107].
  3. At 08:01:00, exactly 60 seconds have elapsed. This is the first second after the green interval, so the output is izchakai.

Why it works

The light’s state repeats every 108 seconds. Normalized remainder therefore identifies the same point in the cycle for any input time. The four disjoint intervals match the stated durations and cover every possible remainder, so classification is exact.

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

Watch out. C++ remainders can be negative. Normalize before classifying and use half-open boundary tests.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
using namespace std;
int main() {
    int hour, minute, second;
    cin >> hour >> minute >> second;
    int elapsed = 3600 * hour + 60 * minute + second - 8 * 3600;
    // Normalize negative offsets for times before 08:00.
    int phase = (elapsed % 108 + 108) % 108;
    if (phase < 60) {
        cout << "premini\n";
    } else if (phase < 64) {
        cout << "izchakai\n";
    } else if (phase < 104) {
        cout << "spri\n";
    } else {
        cout << "izchakai\n";
    }
}

Source: Arena Olimpiici ↗