(SPOJ) 19766 - Arqueólogo Ruim de Matemática - 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;
        int sinal = 1;
        while (true) {
            n = br.read();
            if (n >= '0' && n <= '9') break;
            if (n == '-') sinal = -1;
            if (n == '+') sinal = 1;
        }
        while (true) {
            resp = resp*10 + n-'0';
            n = br.read();
            if (n < '0' || n > '9') break;
        }

        return resp*sinal;
    }
   
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int casos = leitor(br);

        int idade = 0;
        int anoNasc = 0;
        int anoFalec = 0;
       
        int contador = 0;       
        while (casos > contador) {
            anoNasc = leitor(br);
            anoFalec = leitor(br);
           
            if (anoNasc < 0 && anoFalec > 0) {
                idade = (anoFalec + (anoNasc*(-1))) - 1;
            }
            else {
                idade = anoFalec - anoNasc;
                if (idade < 0) {
                    idade *= -1;
                }
            }
           
            bw.write(idade + "\n");
            contador++;
        }  
       
        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