Posts

Showing posts from 2014

(UVA) 10193 - All You Need Is Love - Solução

- A princípio, tentei como solução algo como:             boolean achei = false;             for (int j = 2; j < menor && !achei; j++) {                 if (s1Decimal%j == 0 && s2Decimal%j == 0) {                     achei = true;                 }             } - No entanto, embora a solução esteja correta, houve "Time limit exceeded". - Como solução, tentei encontrar o MDC (Máximo Divisor Comum). O MDC sendo 1, significa que os números são primos entre si e que, desta forma, a condição desejada não existe. 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 >=

Conversão de base

Considere d um valor inteiro na base decimal ... - Para converter um número decimal em binário: String binarioD = Integer.toBinaryString(d); - Para converter um número decimal em hexadecimal: String hexadecimalD = Integer.toHexString(d); Considere b uma String de um valor binário ... - Para converter um número binário em decimal: int decimalB = Integer.parseInt(b, 2); - Para converter um número binário em hexadecimal: int hexadecimalB = Integer.toHexString(decimalB); Considere h uma String de um valor hexadecimal ... - Para converter um número hexadecimal em decimal: int decimalH = Integer.parseInt(h, 16); - Para converter um número hexadecimal em binário: int hexadecimalB = Integer.toBinaryString(decimalH); Fonte de consulta: http://www.activeinfo.com.br/curso_programacao/conversao_entre_bases.html

(UVA) 10019 - Funny Encryption Method - 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 qteCasos = leitor(br);   

(UVA) 458 - The Decoder - Solução

1) A princípio tentei resolver este problema utilizando o Map para fazer as correspondências, mas o resultado foi "Time limit exceeded". 2) Pesquisando, encontrei a postagem http://www.loiane.com/2011/11/uva-problema-458-the-decoder/ que mostra uma forma diferente de resolver o problema sem precisar usar o Map (o que ajuda muito na redução do tempo) e usando o DataInputStream e o DataOutputStream . import java.io.*; import java.util.*; import java.lang.Math; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.takeInput();                System.exit(0);     }        void takeInput() throws NumberFormatException, IOException {         DataInputStream dis = new DataInputStream(System.in);         DataOutputStream dos = new DataOutputStream(System.out);                byte c;                byte leByte;         try {             while (true) {      

(UVA) 12403 - Save Setu - Solução

import java.io.*; import java.util.*; import java.lang.Math; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.takeInput();                 System.exit(0);     }         void takeInput() throws NumberFormatException, IOException {         String line = "";         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 line = br.readLine();                 int numOp = Integer.parseInt(line);                 int saldo = 0;         for (int i = 0; i < numOp; i++) {             line = br.readLine();                         if (line.equals("report")) {                 System.out.println(saldo);             }             else {                 String[] vetor = line.split(" ");                 saldo += Integer.parseInt(vetor[1]);             }                }                               return;     } }

(UVA) 11850 - Alaska - Solução

Atentar para os seguintes fatos: 1) Brenda irá dirigir de Dawson City para Delta Juntion e voltar, ou seja, ela sempre sai da localização 0, vai até a localização 1422 e volta. (Lembrar que na localização 1422 não existe estação de recarga) 2) A entrada, referente à localização das estações, nem sempre será dada em ordem crescente ou decrescente. (Ordenar) 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 < 

(UVA) 11936 - The Lazy Lumberjacks - Solução

Similar ao problema dos triângulos do SPOJ. 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 casos = leitor(br);            

(SPOJ) 19967 - Triângulo - 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 a = leitor(br);         int b = leitor(br);         int c = leitor(br);         int d = leitor(br);            

(SPOJ) 11004 - Triângulos - 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 a = leitor(br);         int b = leitor(br);         int c = leitor(br);                 int qA = a*a;         i

(SPOJ) 19767 - Problema Bão Demais, sô - 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 qteEscolas = leitor(br);         

(SPOJ) 2609 - Trilhas - 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 qteTrilhas = leitor(br);                         int idtrilha = 0;         int trilhaMaisFacil = 1000000001;   

(SPOJ) 19933 - Colchão - Solução

Image
* Verificar uma forma melhor de resolver este problema 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 cProf = leitor(br);         int cLarg = leito

(SPOJ) 18543 - Vende-se - 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 qtePredios = leitor(br);         int qteVenda = leitor(br);                         int[] vetor = new int[qtePredios];         for (in

(SPOJ) 19930 - Frequência na Aula - 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 qteRegistros = leitor(br);                 Map<Integer,Boolean> presenc

(SPOJ) 19939 - Carnaval - 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();         StringTokenizer tokenizer = new StringTokenizer(line);                 float notas[] = new float[5];                 for (int i = 0; i < 5; i++) {             notas[i] = Float.valueOf(tokenizer.nextToken());         }                 Arrays.sort(notas);                 float resultado = 0.0f;         for (int i = 1; i < 4; i++) {             resultado += notas[i];         }         System.out.printf("%.1f\n", resultado);                     return;     }     }

(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 =