ZeroJudge B682: Good Morning

Principal Xia is a principal who enjoys interacting with students, and she is best known for standing at the school gate and saying good morning to the students during school hours.
One day, a student became curious about how long the principal would continue to stand at the school gate. He recorded the time when the principal started standing at the school gate and when the session ended. It's quite remarkable that the principal stood continuously during this period without taking any breaks.
Please help calculate how long the principal stood in total this time.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of two lines.
The first line consists of two integers, H1 and M1, representing the start time when the principal stood at the school gate as H1 hours and M1 minutes.
The second line consists of two integers, H2 and M2, representing the end time when the principal left the school gate as H2 hours and M2 minutes.
The input data satisfies 0≤M1, M2≤59.
Please output a single line containing two integers H and M, separated by a single space, indicating that the principal stood for H hours and M minutes. Ensure that 0≤H≤23 and 0≤M≤59.
7 10
7 20
0 10
7 20
8 15
0 55
8 30
13 40
5 10

Thought Process

Convert both the start and end times to minutes. If the end time is earlier than the start time, it means there's a change of day in between, so add a day's worth of minutes (1440 minutes) to the end time. The answer is then the difference between the end time and the start time. The hours are obtained by dividing the answer by 60, and the minutes are obtained by taking the answer modulo 60.

Sample Code-ZeroJudge B682: Good Morning

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int a, b, start, finish;
    cin >> a >> b;
    start = (a*60) + b;
    cin >> a >> b;
    finish = (a*60) + b;
    if (start > finish) finish += 1440;
    int ans = finish - start;
    cout << ans/60 << " " << ans%60 << "\n";
}

//ZeroJudge B682
//Dr. SeanXD

Comments