668. Game Time 2

668. Game Time 2

Read four integers A, B, C, D to indicate the start time and end time of the game.

Where A and B are the hours and minutes at the start time, and C and D are the hours and minutes at the end time.

Please calculate the duration of the game.

The minimum duration of the competition is 11 minutes and the maximum duration is 24 hours.

Input format

A total of one line, containing four integers A, B, C, D.

Output format

The output format is O JOGO DUROU X HORA(S) E Y MINUTO(S), which means that the game lasted for X hours and Y minutes in total.

data range

0≤A,C≤23,
0≤B,D≤59

Input example 1:

7 8 9 10

Output sample 1:

O JOGO DUROU 2 HORA(S) E 2 MINUTO(S)

Input example 2:

7 7 7 7

Output sample 2:

O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)

Input sample 3:

7 10 8 9

Output sample 3:

O JOGO DUROU 0 HORA(S) E 59 MINUTO(S)
// 正常思路做会出现小时、分钟两种借位的情况,
// 如果一开始就把开始结束时间先换算成分钟,就只有开始分钟数大于结束分钟数这一种情况,
// 这种情况直接在 b - a 基础上再+ 24 * 60,最后把过程分钟数转换成标准格式即可。




#include <cstdio>

int main()
{
	int a, b, c, d, th, ts;
	scanf("%d%d%d%d", &a, &b, &c, &d);
	
	if (a <= c)
	{		
		if (b <= d) 
		{
			th = c - a;	
			ts = d - b;	
		}
		else 
		{
			if (a == c)
			{
				th = 23;
				ts = 60 + d - b;
			}
			else
			{				
				th = c - a -1;	
				ts = d - b + 60;
			}	
		}		
	}
	else 
	{
		if (b <= d) 
		{
			th = 24 - a + c;	
			ts = d - b;	
		}
		else 
		{
			th = 24 - a + c -1;	
			ts = d - b + 60;	
		}
	}
	
	if (th == 0 && ts == 0) th =24;
	printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", th, ts);
	
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_42465670/article/details/115069322