(SPOJ) 5474 - Alarme Despertador - 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);
    }
   
    static int leitor(BufferedReader br) throws NumberFormatException, IOException {
        int n;
        int resp = 0;
        while (true) {
            n = br.read();
            if (n >= '0' && n <= '9') {
                break;
            }
        }
        while (true) {
            resp = resp*10 + n-'0';
            n = br.read();
            if (n < '0' || n > '9') {
                break;
            }
        }
        return resp;
    }
   
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
       
        int hInicial = leitor(br);
        int mInicial = leitor(br);
        int hFinal = leitor(br);
        int mFinal = leitor(br);
       
        while(hInicial != 0 || mInicial != 0 || hFinal != 0 || mFinal != 0) {
            int dHora = 0;
            int dMin = 0;
            int minutos = 0;
           
            if (hInicial == hFinal) {
                if (mInicial > mFinal) {
                    dHora = 23 * 60;
                    dMin = 60 - mInicial + mFinal;
                    minutos = dHora + dMin;
                }
                else {
                    minutos = mFinal - mInicial;
                }
            }
            else if (hInicial < hFinal) {
                if (mInicial > mFinal) {
                    dHora = (hFinal-hInicial-1) * 60;
                    dMin = 60 - mInicial + mFinal;
                    minutos = dHora + dMin;
                }
                else {
                    dHora = (hFinal-hInicial) * 60;
                    dMin = mFinal - mInicial;
                    minutos = dHora + dMin;
                }
            }
            else if (hInicial > hFinal) {
                if (mInicial > mFinal) {
                    dHora = ((24-hInicial+hFinal)-1) * 60;
                    dMin = 60 - mInicial + mFinal;
                    minutos = dHora + dMin;
                }
                else {
                    dHora = (24-hInicial+hFinal) * 60;
                    dMin = mFinal - mInicial;
                    minutos = dHora + dMin;
                }
            }
           
            bw.write(minutos+"\n");
           
            hInicial = leitor(br);
            mInicial = leitor(br);
            hFinal = leitor(br);
            mFinal = leitor(br);
        }
       
        bw.flush();
        bw.close();           
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Powers of Two - Solução

(CoderByte) Number Search - Solução