Algorithm
Game Time with Minutes
Adapted by Neilor Tonin, URI Brazil
Read the start time and end time of a game, in hours and minutes (initial hour, initial minute, final hour, final minute). Then print the duration of the game, knowing that the game can begin in a day and finish in another day,
Obs.: With a maximum game time of 24 hours and the minimum game time of 1 minute.
Input
Four integer numbers representing the start and end time of the game.
Output
Print the duration of the game in hours and minutes, in this format: “O JOGO DUROU XXX HORA(S) E YYY MINUTO(S)” . Which means: the game lasted XXX hour(s) and YYY minutes.
Input Sample | Output Sample |
7 8 9 10 |
O JOGO DUROU 2 HORA(S) E 2 MINUTO(S) |
7 7 7 7 |
O JOGO DUROU 24 HORA(S) E 0 MINUTO(S) |
7 10 8 9 |
O JOGO DUROU 0 HORA(S) E 59 MINUTO(S) |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <studio.h>
int main()
{
int start_h, end_h, start_m, end_m, duration_h, duration_m;
scanf("%d %d %d %d", &start_h, &start_m, &end_h, &end_m);
duration_h = end_h - start_h;
if (duration_h < 0)
{
duration_h = 24 + (end_h - start_h);
}
duration_m = end_m - start_m;
if (duration_m < 0)
{
duration_m = 60 + (end_m - start_m);
duration_h--;
}
if (start_h == end_h && start_m == end_m)
{
printf("O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)n");
}
else printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)n", duration_h, duration_m);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
using namespace std;
int main()
{
int hi, mi, hf, mf;
int ctrh = 0, ctrm = 0;
cin >> hi >> mi >> hf >> mf;
if(hi == hf && mi == mf){
cout << "O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)" << endl;
return 0;
}
if(hi == hf && mi != mf){
hi++;
while(mi != mf)
{
ctrm++;
mi++;
if(mi == 61)
mi = 1;
}
while(hi != hf)
{
ctrh++;
hi++;
if(hi == 25)
hi = 1;
}
}
while(mi != mf)
{
ctrm++;
mi++;
if(mi == 61){
mi = 1;
ctrh--;
}
}
while(hi != hf)
{
ctrh++;
hi++;
if(hi == 25)
hi = 1;
}
cout << "O JOGO DUROU " << ctrh << " HORA(S) E " << ctrm << " MINUTO(S)" << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int start_h, end_h, start_m, end_m, duration_h, duration_m;
Scanner sc = new Scanner(System.in);
start_h = sc.nextInt();
start_m = sc.nextInt();
end_h = sc.nextInt();
end_m = sc.nextInt();
duration_h = end_h - start_h;
if (duration_h < 0)
{
duration_h = 24 + (end_h - start_h);
}
duration_m = end_m - start_m;
if (duration_m < 0)
{
duration_m = 60 + (end_m - start_m);
duration_h--;
}
if (start_h == end_h && start_m == end_m)
{
System.out.printf("O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)n");
}
else
System.out.printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)n", duration_h, duration_m);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
a,c,b,d=list(map(int,input().split()))
dif=((b*60)+d)-((a*60)+c)
if(dif<=0):
dif+=24*60
time=dif//60
minute=dif%60
print(f"O JOGO DUROU {time} HORA(S) E {minute} MINUTO(S)">
Copy The Code &
Try With Live Editor
Input
Output