URI Online Judge | 1047 Game Time with Minutes (Solution)
Problem Link:
https://www.urionlinejudge.com.br/judge/en/problems/view/1047
https://www.urionlinejudge.com.br/judge/en/problems/view/1047
URI Online Judge | 1047
Game Time with Minutes
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
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.
Sample Input | Sample Output |
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) |
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| #include <stdio.h> int main() { int st, sm, et, em, rm, rt; scanf ( "%d %d %d %d" , &st, &sm, &et, &em); rt = et - st; if (rt < 0) { rt = 24 + (et - st); } rm = em - sm; if (rm < 0) { rm = 60 + (em - sm); rt--; } if (et == st && em == sm) { 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" , rt, rm); return 0; } |
This comment has been removed by the author.
ReplyDeletethat solution is terrible
ReplyDeletetry this:
ReplyDelete#include
int main() {
int hi, hf, mi, mf;
scanf("%d%d%d%d", &hi, &mi, &hf, &mf);
int m = (hf*60 + mf) - (hi*60 + mi);
if ( m <= 0 ) m += 1440; //24*60
int h = m/60;
m %= 60;
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", h, m);
return 0;
}
it's osam :-)
DeleteFantastic. It's works for me
Deleteor this one:
ReplyDelete#include
int p1047() {
int hi, hf, mi, mf;
scanf("%d%d%d%d", &hi, &mi, &hf, &mf);
int h = hf - hi;
int m = mf - mi;
if ( h <= 0 && m <= 0) h += 24;
if ( m < 0 ) {
h--;
m += 60;
}
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", h, m);
return 0;
}
this solution is not valid,,because if you enter input 7 7 7 6 then its out put -1 hour 59 minutes
ReplyDelete#include
ReplyDelete#include
using namespace std;
int main()
{
int initial_hour , initial_minute , final_hour , final_minute , minutes , hours;
cin >> initial_hour >> initial_minute >> final_hour >> final_minute ;
hours = final_hour - initial_hour ;
if(hours < 0) {
hours = 24 + (final_hour - initial_hour);
}
minutes = final_minute - initial_minute ;
if(minutes < 0) {
minutes = 60+(final_minute - initial_minute) ;
hours--;
}
if (final_hour == initial_hour && final_minute == initial_minute) {
cout << "O JOGO DUROU 24 HORA(S) E 0 MINUTO(S)" << endl ;
}
else {
cout << "O JOGO DUROU " << hours <<" HORA(S) E " <<minutes<<" MINUTO(S)" << endl ;
}
return 0;
}
A BEGINNER'S SOLUTION!!!
ReplyDelete#include
int main()
{
int sh, sm, eh, em, n, n1, n2, n3,n4,n5,n6,n7;
scanf("%d %d %d %d", &sh, &sm, &eh, &em);
if(eh > sh && em > sm){
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", eh - sh, em - sm);
}
else if(eh > sh && em < sm){
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", ((eh - 1) - sh), (em + 60) - sm);
}
else if(sh > eh && sm > em){
n = 23 - sh;
n1 = 60 - sm;
if(n1 + em >= 60){
n2 = (n + eh) + 1;
n3 = (n1 + em)% 60;
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", n2, n3);
}
else{
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", n + eh, n1 + em);
}
}
else if(sh > eh && sm < em){
n4 = 23 - sh;
n5 = 60 - sm;
if((n5 + em)>= 60){
n6 = n4 + eh + 1;
n7 = (n5 + em)% 60;
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", n6, n7);
}
else{
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", n4 + eh, n5 + em);
}
}
else if(sh == eh && sm == em){
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", 24, 0);
}
else if(sh == eh && em > sm){
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", 0, em - sm);
}
else if(sh == eh && sm > em){
printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", 23, (em - sm) + 60);
}
return 0;
}