Interval – solution
We convert all four times into minutes after midnight (hours·60 + minutes). The time spent together starts when the person who arrived later comes, and ends when the person who left earlier goes. If that start is after the end, they did not meet. At the end, the result in minutes is split into hours (/ 60) and minutes (% 60).
#include <iostream>
using namespace std;
/*
Pete and Ann work in the same software company. If the working hours of each of
them are known, write the program INTERVAL that determines how much time they
spent at work together. The input contains the time when Pete came to work, the
time when he left, the time when Ann came to work and the time when she left.
Each time is given in a separate line, as two numbers separated by one space.
Print the result as a number of hours and minutes, again separated by one
space.
Input: Output:
8 50 5 5
16 40
9 20
14 25
*/
int main()
{
int pHA,pMA,pHL,pML;
int aHA,aMA,aHL,aML;
int mTogether=0,hTogether;
cin >> pHA >> pMA;
cin >> pHL >> pML;
cin >> aHA >> aMA;
cin >> aHL >> aML;
pMA = pHA*60+pMA;
pML = pHL*60+pML;
aMA = aHA*60+aMA;
aML = aHL*60+aML;
if(pMA <= aMA){ //Pete came first
if(pML<=aMA){//Pete also left before Ann came
//they do not meet
mTogether=0;
}
else{
mTogether=min(aML,pML)-aMA;
}
}
else if(pMA>=aMA){ //Ann came first
if(aML<=pMA){
//they do not meet
mTogether=0;
}
else{
mTogether=min(aML,pML) - pMA;
}
}
hTogether=mTogether/60;
mTogether=mTogether%60;
cout<<hTogether<<" "<<mTogether;
return 0;
}
