Home > front end >  Can anyone help me to find the mistake of this code (URI 1047)?
Can anyone help me to find the mistake of this code (URI 1047)?

Time:08-01

Uri 1047 (Game time with Minutes)

Can anyone help me, I don't see any mistake in my code. But when I submit it to URI it's showing wrong ans. But what's wrong in my code?

enter image description here

#include <stdio.h>

int main ()
{
    int h1, h2, m1, m2, start, end, h, m, X;

    scanf ("%d%d%d%d", &h1, &m1, &h2, &m2);

    if (h1 > h2) {
        h2 = h2   24;
    }
    
    start = (h1 * 60)   m1;
    end = (h2 * 60)   m2;

    X = end - start;

    h = X / 60;
    m = X % 60;

    if (X == 0) {
        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", h, m);
    }
    
    return 0;
}

CodePudding user response:

This should work:

if(h1 > h2 || ( h1 == h2 && m1 >= m2 ) ) {

In the (edge) case of start: 15:20 end: 15:15 your conditional would not recognise elapsed time of 23:55...

Here's a more compact version...

void main() {
    int h1, h2, m1, m2;

    scanf( "%d%d%d%d", &h1, &m1, &h2, &m2 );

    int start = (h1 * 60)   m1;
    int end = (h2 * 60)   m2;

    if( start >= end )
        end  = 24 * 60;

    int X = end - start;
    int h = X / 60;
    int m = X % 60;

    printf("O JOGO DUROU %d HORA(S) E %d MINUTO(S)\n", h, m);
}
  •  Tags:  
  • c
  • Related