Connection Status:
Competition Arena > BadClock
SRM 172 · 2003-11-20 · by Eeyore · Math
Class Name: BadClock
Return Type: double
Method Name: nextAgreement
Arg Types: (string, string, int)
Problem Statement

Problem Statement

According to Lewis Carroll, a clock that has stopped is more accurate than one that is five minutes behind. He argues that the former is right twice a day, whereas the latter never shows the correct time. Then again, a clock that is always five minutes behind is in a sense perfectly accurate, and therefore an extraordinary specimen. More usually, a clock is ahead or behind because it runs at the wrong rate, so that its absolute discrepancy from the true time is steadily changing. If left unregulated, such a clock will show the true time at regular but perhaps lengthy intervals.

You are given two Strings of the form "hh:mm:ss". The first represents exactly the true time, while the second is exactly the time shown by an ill-tuned clock. This is an analog clock whose hour, minute, and second hands sweep continuously around the dial at a speed that is too fast or too slow by a constant factor. Both times are given in the North American style, where the hour ranges between 1 and 12, inclusive. Given an int specifying the non-zero number of seconds that the clock gains every hour, calculate the number of hours that elapse before it agrees with the true time. Your answer, a double, must be correct with either absolute or relative precision of 1.0e-9 (one billionth).

Notes

  • If hourlyGain is negative, the clock falls behind by a fixed number of seconds every hour.
  • It is not the case that the clock makes discrete jumps every so often. Rather, the hands of the clock are moving smoothly and continuously at a constant rate that is too slow or too fast relative to the true time.

Constraints

  • trueTime and skewTime each contain exactly eight characters in the format "hh:mm:ss", where the substring "hh" is a zero-padded integer between 1 and 12, inclusive, and "mm" and "ss" are zero-padded integers between 0 and 59, inclusive
  • hourlyGain is either between -1800 and -1, inclusive, or between 1 and 3600, inclusive
Examples
0)
"07:07:07"
"07:07:07"
1776
Returns: 0.0

The clock is already showing the true time.

1)
"11:59:58"
"12:03:28"
-3
Returns: 70.0

This clock loses three seconds every hour, and will catch up with the true time in exactly 70 hours.

2)
"12:03:28"
"11:59:58"
3
Returns: 70.0

This clock gains three seconds per hour.

3)
"03:03:02"
"03:01:47"
5
Returns: 15.0
4)
"03:03:02"
"03:01:47"
-5
Returns: 8625.0

Submissions are judged against all 121 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class BadClock with a public method double nextAgreement(string trueTime, string skewTime, int hourlyGain) · 121 test cases · 2 s / 256 MB per case

Submitting as anonymous