Problem in issue 1046 of the URI site

Asked

Viewed 166 times

0

I am getting a compilation error (Compilation error) in the URI site judgment on issue 1046, although it is working perfectly in my Code Blocks. Question: inserir a descrição da imagem aqui

My code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int hr_inicial, hr_final, duracao;
    scanf("%d %d", &hr_inicial, &hr_final);

if((hr_inicial <= 24) && (hr_inicial >= 0) && (hr_final <= 24) && (hr_final >= 0)){
    if(hr_inicial == 24){
        hr_inicial = 0;
    }
    if(hr_inicial < hr_final){
        duracao = hr_final - hr_inicial;
    }
    if (hr_inicial > hr_final){
        for(int i = 1; i <= 24; i++){
            hr_inicial++;
            if(hr_inicial == 24){
                duracao = i;
                i = 1;
            }
            if(i == hr_final){
                    duracao += i;
            }
        }
    }
    if(hr_inicial == hr_final){
        duracao = 24;
    }
    printf("O JOGO DUROU %d HORA(S)\n", duracao);
}

return 0;
}

What can I have wrong?

  • 1

    But it is not enough to subtract the initial hr_start from the final hr_and if it is negative add 24?

  • Damn, I should have thought of it, that way it was accepted and did not involve as much logic and code as in my resolution, thanks!

1 answer

0

here is the resolution:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int hr_inicial, hr_final, duracao;
    scanf("%d %d", &hr_inicial, &hr_final);

        if(hr_inicial < hr_final){
            duracao = hr_final - hr_inicial;
        }
        if(hr_inicial > hr_final){
            duracao = (hr_final - hr_inicial) +24;
        }
        if(hr_inicial == hr_final){
            duracao = 24;
        }
        printf("O JOGO DUROU %d HORA(S)\n", duracao);

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.