Official

A - When? Editorial by en_translator


Finding the answer

Suppose that the answer is \(M\) minutes past \(H\) in an \(24\)-hour clock.

Since \(0 \leq X \leq 100\), \(H=21\) if \(X \lt 60\) and \(H = 22\) if \(X \geq 60\). Also, \(M\) equals the remainder when \(X\) is divided by \(60\).

Printing the answer

If the hour or the minute is a one-digit number, you have to prepend a \(0\) to output it as a \(2\)-digit number. Here is a tutorial on how to implement this in C++ / Python.

Sample codes in C++

1. Defining an original function

It is sufficient to define a function that receives an integer as an argument, pads zero if necessary, and returns a string of length \(2\).

#include <iostream>
#include <string>
using namespace std;

string fix(int x) {
  if (x < 10) {
    return string{'0'} + to_string(x);
  } else {
    return to_string(x);
  }
}

int main() {
  int X;
  cin >> X;
  int H = X < 60 ? 21 : 22;
  int M = X % 60;
  cout << H << ':' << fix(M) << '\n';
}
2. Using std::setfill and std:::setw

Using these functions enables to specify the width of the output and to set the character to be used as the pad.

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  int X;
  cin >> X;
  int H = X < 60 ? 21 : 22;
  int M = X % 60;
  cout << H << ':' << setw(2) << setfill('0') << M << '\n';
  return 0;
}
3. Using std::printf

In this problem, you can use std::printf instead of std::cout to write a simpler code. From C++20, std::format is introduced in the standard library, which provides advanced format specifiers.

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
  int X;
  cin >> X;
  int H = X < 60 ? 21 : 22;
  int M = X % 60;
  printf("%d:%02d", H, M);
  return 0;
}

Sample codes in Python

Using str.format()

format method enables advanced format specifiers of strings. Zero-pad can be processed simply as well.

x = int(input())
h = 21 if x < 60 else 22;
m = x % 60
print('{}:{:02}'.format(h, m))
Using datetime

Standard date-time library datetime provides features of calculations and output of date and time.

from datetime import datetime, timedelta
begin = datetime(2022, 7, 2, hour=21)
delta = timedelta(minutes=int(input()))
answer = begin + delta
print(answer.strftime('%H:%M'))

posted:
last update: