(SPOJ) 18285 - Ano Novo - Solução

import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.processa();
       
        System.exit(0);
    }
   
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        String line = "";
       
        int horas;
        int difHora;
       
        int minutos;
        int difMinutos;
       
        int segundos;
        int difSegundos;
       
        int resultado;
       
        while ((line = br.readLine()) != null) {
            String[] vetor = new String[3];
           
            vetor = line.split(":");
           
            horas = Integer.parseInt(vetor[0]);
            minutos = Integer.parseInt(vetor[1]);
            segundos = Integer.parseInt(vetor[2]);
           
            // 1m tem 60s
            // 1h tem 3600s
            // 24h tem 24x3600 = 86400s
            if (horas == 0 && minutos == 0 && segundos == 0) { // 00:00:00
                bw.write("86400\n");
            }
            else if (minutos == 0 && segundos == 0) { // xx:00:00
                resultado = (24-horas) * 3600;
                bw.write(resultado + "\n");
            }
            else {
                difHora = 23 - horas;
               
                if (minutos != 0) {
                    difMinutos = 59 - minutos;
                }
                else { // minutos == 00
                    difMinutos = 59;
                }
               
                if (segundos != 0) {
                    difSegundos = 60 - segundos;
                }
                else { // segundos == 00
                    difSegundos = 60;
                }
                       
                resultado = (difSegundos + 60 * difMinutos + 3600 * difHora);

                bw.write(resultado + "\n");
            }
           
        }
                       
        bw.flush();
        bw.close();           
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução