B - Misjudge the Time Editorial by en_translator
The theme of this problem is implementation.
First, let us consider how to determine if the time \(h\):\(m\) is confusing.
It can be determined by the following procedure. (For more details, please refer to the sample code.)
- Find the tens place \(A\) and ones place \(B\) of \(h\) using a integral division (
/
in C++ and//
in Python) and a modulus operation (%
). - Find the tens place \(C\) and ones place \(D\) of \(m\) in the same way.
- Find \(\mathrm{AC}\) and \(\mathrm{BD}\) using additions and multiplications.
- Determine if \(\mathrm{AC}\):\(\mathrm{BD}\) is valid in the \(24\)-hour system. This can be determined by checking if \(0 \leq \mathrm{AC} \leq 23\) and \(0 \leq \mathrm{BD} \leq 59\).
- The resulting boolean value (Yes or No) directly serves as the answer to “is \(h\):\(m\) confusing?”
Once you can determine if a time is confusing, this problem can be solved with a loop structure like a while
statement. That is, it is sufficient to repeat incrementing the time by \(1\) as long as \(H\):\(M\) is not a confusing time.
Note that, the one minute after \(H\):\(M\) is \(H\):\((M+1)\) in most case, there may be carries, when it causes \(H=24\) or \(M=60\).
The time complexity is \(\mathrm{O}( \text{(the number of distinct times represented by 24-hour clock system)})\). Since they are \(1440\) such times, so we can determine it fast enough.
A sample code in Python follows.
def is_in_24_hours(h, m):
return 0 <= h <= 23 and 0 <= m <= 59
def misjudged(h, m):
A, B = h // 10, h % 10
C, D = m // 10, m % 10
AC = A * 10 + C
BD = B * 10 + D
return is_in_24_hours(AC, BD)
H, M = map(int, input().split())
while not misjudged(H, M):
M += 1
if M == 60:
H, M = H + 1, 0
if H == 24:
H = 0
print(H, M)
posted:
last update: