Posts

Showing posts from October, 2014

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

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 minutos;         int segundos;                 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

(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.pars

(SPOJ) 813 - Saldo de gols - Solução 2

Apenas a implementação utilizando o leitor de inteiros, e não o StringTokenizer. 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 Out

(SPOJ) 1821 - O Bolo de Apostas - 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 Bu

(SPOJ) 20001 - Fila - Solução

Image
Utilizado ArrayList, uma vez que com a utilização do vetor comum, o tempo limite era excedido. O ArrayList possui o método remove() que, em uma de suas funções, permite excluir a primeira ocorrência de um elemento específico. 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 NumberFo

(SPOJ) 19964 - Telefone - 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));         Map<Character,Character> traducao = new HashMap<Character,Character>();                 traducao.put('A', '2');         traducao.put('B', '2');         traducao.put('C', '2');         traducao.put('D', '3');         traducao.put('E', '3');         traducao.put('F', '3');         traducao.put('G', '4');         traducao.put('H', '4');         traducao.put('I', '4');

(SPOJ) 19989 - Conversa Secreta - 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 = "";                 char[] vetor = new char[256];                 vetor[(int)'n'] = 'a';         vetor[(int)'o'] = 'b';         vetor[(int)'p'] = 'c';         vetor[(int)'q'] = 'd';         vetor[(int)'r'] = 'e';         vetor[(int)'s'] = 'f';         vetor[(int)'t'] = 'g';         vetor[(int)'u'] = 'h';         vetor[(int)'v'] = 'i';   

(SPOJ) 19947 - Língua do P - Solução

import java.io.*; import java.util.*; import java.math.*; 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 = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);                 String resultado = "";         String palavra = "";         int tamanho;         int contador = 0;         while (tokenizer.hasMoreTokens()) {             if (contador != 0) {                 resultado += " ";             }             else {                 contador = 1;             }                         palavra = tokenizer.nextToken();             tamanho =

(SPOJ) 18546 - Volume da TV - Solução

Image
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));         int volume = leitor(br);         i

(SPOJ) 19999 - Flíper - Solução

Image
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));         int p = leitor(br);         int r = leitor(br);                 if (p == 0) {             System.out.println("C");         }         else {    

(SPOJ) 19963 - OBI - Solução

Image
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));         int numCompetidores = leitor(br);         int minPontos = leitor(br);                 int nota1;         int nota2;                 int contaConvidados =

(SPOJ) 18519 - Saldo do Vovô - Solução

Image
import java.io.*; import java.util.*; import java.math.*; 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));         int numDias

(SPOJ) 19970 - Letras - Solução

Image
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));                 String line = br.readLine();         char letra = line.charAt(0);         boolean possuiLetraEspecial = false;                 String palavra = "";         int palavraComLetra = 0;                 line = br.readLine(); // lê linha com a frase         StringTokenizer tokenizer = new StringTokenizer(line);                 int contaTokens = tokenizer.countTokens();         for (int j = 0; j < contaTokens; j++) {             palavra = String.valueOf(tokenizer.nextToken());             int tamPalavra = palavra.length();             for (int i = 0; i < tamPalavra; i++) {

(SPOJ) 20671 - Senha da Tia - 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 minCaracteres = leitor(br);

(SPOJ) 19962 - Insensibilidade - Solução

Image
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 qteObjetos = leitor(br);         int x1;         i

(SPOJ) 19940 - Consecutivos - Solução

Image
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

(SPOJ) 20008 - Tira-teima - Solução

Image
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

(SPOJ) 19959 - Peça Perdida - 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));                 int qtePecas = leitor(br);                 int[] vetor = new int[qtePecas+1];                 int numero;  

(SPOJ) 19941 - Corrida - 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 long leitor(BufferedReader br) throws NumberFormatException, IOException {         long n;         long 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));                 System.out.println(leitor(br)%leitor(br));                                                return;     }

(SPOJ) 20005 - Chocolate - Solução

Image
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 tamanho = leitor(br);                    int resultado = 1;       

(SPOJ) 2285 - Krakóvia - Solução

import java.io.*; import java.util.*; import java.math.*; 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));         String line = &q

(SPOJ) 18537 - Corrida - Solução

Image
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));        

(SPOJ) 20000 - Gangorra - 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 res1 = leitor(br) * leitor(br);         int res2 = leito