Description
How many hours have he slept in total?
Input has multiple groups of test data, one row in each group, which is the bed time and the bed time. A space is separated between them. Output: for each group of data, the total sleep time of the coders is T (minutes) of course 0 <= T <24*60 sample input01: 00 08: 00 sample output420 540
# Include <algorithm> # include <iostream> # include <string> # include <vector> using namespace STD; int main () {string S1, S2; while (CIN> S1> S2) {int H1 = (S1 [0]-'0') * 10 + (S1 [1]-'0 '); int H2 = (s2 [0]-'0') * 10 + (s2 [1]-'0'); int M1 = (S1 [3]-'0 ') * 10 + (S1 [4]-'0'); int m2 = (s2 [3]-'0 ') * 10 + (s2 [4]-'0'); int flag = 0; // whether to borrow an hour int sum = 0; If (M1> m2) {flag = 1; sum + = (M2 + 60-M1);} else {sum + = (m2-M1);} If (H2> = (H1 + flag )) // equal sign do not forget Oh, critical judgment should be careful {sum + = (h2-h1-flag) * 60);} else {sum + = (H2 + 24-h1-flag) * 60) ;}cout <sum <Endl ;}}