Calculate difference between two schedules

Asked

Viewed 198 times

-1

I’m having second thoughts about the logic of this code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int horaInicial = sc.nextInt();
        int minutoInicial = sc.nextInt();
        int horaFinal = sc.nextInt();
        int minutoFinal = sc.nextInt();
        sc.close();
        int instanteInicial = horaInicial * 60 + minutoInicial;
        int instanteFinal = horaFinal * 60 + minutoFinal;
        int duracao;
        if (instanteInicial < instanteFinal) {
            duracao = instanteFinal - instanteInicial;
        }
        else {
            duracao = (24 * 60 - instanteInicial) + instanteFinal;
        }
        int duracaoHoras = duracao / 60;
        int duracaoMinutos = duracao % 60;
        System.out.println("O JOGO DUROU " + duracaoHoras + " HORA(S) E " + duracaoMinutos + " MINUTO(S)");
    }
}

The code is equivalent to uri1047, I’m having trouble understanding the resolution code quoted, only on the line:

duracao = (24 * 60 - instanteInicial) + instanteFinal;

I don’t understand why subtract instanteInicial 1440 (24 * 60) and then add instanteFinal, I know 1440 equals 1 day in minutes.

  • 2

    You don’t understand the code you made?

  • I wasn’t being able to solve that problem, so I researched a resolution in git and found this one and I’d like to understand it,;

  • 1

    It’s because - from what I understand - the game can end the next day. For example, if it starts at 10:00 and ends at 3:00 in the morning (the next day, I suppose), then it enters the else and to compensate for the fact that the final hour is shorter (3 < 22), it adds another 24 hours (to "compensate", or to "add" the hours of the next day). And I didn’t really need those parentheses, they’re just there to confuse...

  • 1

    That being said, I don’t really like online URI because many problems are like this: they don’t give all the requirements and you have to keep guessing the situations that it will test, as is the case here. You can even "guess" this criterion based on examples, but the statement would have to be clearer, in my opinion, otherwise it becomes more of a game than actually learning (even more for a site that claims to have the goal of "provide Programming Practice and Knowledge sharing").

1 answer

3


The line of code in question could have been clearer if it had been written like this:

duracao = (24 * 60 + instanteFinal) - instanteInicial;

Note that it is totally equivalent to what was written above (changing only the order of sum and subtraction). What this line does then is this: if the final instant appears to be less than the initial instant, it probably belongs to the next day and not to the same day as the initial instant. To accomplish this, just add a day to the final instant (thus displacing it one day in the future).

Browser other questions tagged

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