UVa 11677 – Alarm Clock
Daniela works as a nurse in a large hospital, and her work hours often vary. To make matters worse, she sleeps deeply, and it's hard to wake her up with an alarm clock.
She recently received a digital clock with multiple alarm sounds, hoping it could solve her problem. Due to recent fatigue, she wants to make good use of her rest time. She carries this alarm clock with her, setting the time she needs to wake up whenever she has a break and tries to sleep. However, the more anxious she becomes to fall asleep, the harder it is for her to do so.
She's been wondering how many minutes of sleep she could get if she could fall asleep immediately and before the alarm goes off. But her arithmetic isn't good, so please write a program to calculate how many minutes she can sleep based on the current time and the alarm time.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF inputs: each test case consists of a single line containing four integers: H1, M1, H2, and M2. H1:M1 represents the current "hour and minute."). H2:M2 represents the time set for the alarm "hour and minute."). (0 ≤ H1, H2 ≤ 23, 0 ≤ M1, M2 ≤ 59) The last line contains four zeros separated by spaces, indicating the end of the input. | For each set of data, your program should output the number of minutes Daniela can sleep as a separate line. |
1 5 3 5 23 59 0 34 21 33 21 10 0 0 0 0 | 120 35 1417 |
Thought Process
You can convert both times into the smallest unit (minutes). When the current time is greater than the alarm time, it means it will go through the next day, so you need to add one day (24*60). Finally, output the difference between the alarm time in minutes and the current time in minutes.
Sample Code-ZeroJudge D669: Alarm Clock
#include <iostream>
using namespace std;
int main() {
int h1, m1, h2, m2;
while (cin >> h1 >> m1 >> h2 >> m2)
{
if (h1 == 0 && m1 == 0 && h2 == 0 && m2 == 0) break;
int one, two;
if (h1 == 0) one = (24*60) + m1;
else one = (60*h1) + m1;
if (h2 == 0) two = (24*60) + m2;
else two = (60*h2) + m2;
if (two < one) two += (60*24);
cout << two - one << "\n";
}
}
//ZeroJudge D669
//Dr. SeanXD