Posts

Showing posts from November, 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;     }     }