Posts

Showing posts from December, 2013

(SPOJ) 3828 - Primo - Solução

import java.io.*; import java.util.*; import java.lang.Math; 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         line = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);         int numero = Integer.valueOf(tokenizer.nextToken());         boolean primo = true;         for (int i = 2; i*i <= numero; i++) {             if (numero%i == 0) {                 primo = false;             }         }                 if (primo) {             System.out.println("sim");         }         else {             System.out.println("nao");         }                                 r

(SPOJ) 840 - Cofrinhos da Vó Vitória - Solução

import java.io.*; import java.util.*; import java.lang.Math; 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         int contador = 0;                while((line = br.readLine()) != null) {             StringTokenizer tokenizer = new StringTokenizer(line);             int qte = Integer.valueOf(tokenizer.nextToken());             if (qte == 0) {                 return;             }                         contador++;             System.out.println("Teste " + contador);                         int soma1 = 0;             int soma2 = 0;             for (int i = 0; i < qte; i++) {                 line = br.re

(SPOJ) 8697 - Pneu - Solução

import java.io.*; import java.util.*; import java.lang.Math; 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 line = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);         int valor1 = Integer.valueOf(tokenizer.nextToken());         line = br.readLine();         tokenizer = new StringTokenizer(line);         int valor2 = Integer.valueOf(tokenizer.nextToken());                             System.out.println(valor1-valor2);                     return;     } }

(SPOJ) 3774 - Fatorial - Solução

import java.io.*; import java.util.*; import java.lang.Math; 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 line = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);                 int valor = Integer.valueOf(tokenizer.nextToken());                     int multiplica = 1;         for (int i = 1; i <= valor; i++) {             multiplica *= i;         }                 System.out.println(multiplica);                     return;     } }

(SPOJ) 3829 - Quadrados - Solução

import java.io.*; import java.util.*; import java.lang.Math; 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 line = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);                 int valor = Integer.valueOf(tokenizer.nextToken());                     System.out.println(valor*valor);                     return;     } }

(SPOJ) 1387 - Transmissão de Energia - Solução

import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.takeInput();                 System.exit(0);     }         void buscaProf(int[][] matriz, ArrayList<Integer> visitados, int qteEstacoes, int ponto) {         visitados.set(ponto, 1);         for (int i = 0; i < qteEstacoes; i++) {             if (matriz[ponto][i] == 1) {                 if (visitados.get(i) == 0) {                     buscaProf(matriz, visitados, qteEstacoes, i);                 }             }         }             }         void takeInput() throws NumberFormatException, IOException {         String line = "";         StringTokenizer tokenizer = null;                 int contador = 0;                 int qteEstacoes;         int qteLigacoes;                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 while ((li

(UVA) 10116 - Robot Motion - Solução

import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.takeInput();                System.exit(0);     }     char[][] leMapa(BufferedReader br, int linha, int coluna) throws NumberFormatException, IOException  {         char[][] matrizChar = new char[linha][coluna];         String conteudoLinha;                for (int i = 0; i < linha; i++) {             conteudoLinha = br.readLine();             for (int j = 0; j < coluna; j++) {                 matrizChar[i][j] = conteudoLinha.charAt(j);                       }         }                return matrizChar;         }        void takeInput() throws NumberFormatException, IOException {         String line = "";         StringTokenizer tokenizer = null;                int tamLinha;         int tamColuna;         int inicial;                char[][] ma

(UVA) 11332 - Summing Digits - 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 resultado = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                         while ((resultado = br.readLine()) != null) {             if (resultado.charAt(0)-'0' == 0) {                 return;             }                           Integer tamanho = resultado.length();             Integer soma;                                  Integer i;             do {                 soma = 0;                                 for (i = 0; i < tamanho; i++) {                     soma += (resultado.charAt(i)-'0');                 }       

(UVA) 10137 - The Trip - Solução

import java.io.*; import java.util.*; import java.lang.Math; import java.text.NumberFormat; import java.text.DecimalFormat;   import java.text.DecimalFormatSymbols; import java.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));                 while ((line = br.readLine()) != null) {             StringTokenizer tokenizer = new StringTokenizer(line);             Integer qteEntradas = Integer.parseInt(tokenizer.nextToken());                         ArrayList<Integer> qteGasta = new ArrayList<Integer>();                         if (qteEntradas == 0) {                 return;             }    

(UVA) 10082 - WERTYU - 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));                 Map<Character, Character> teclado = new TreeMap<Character, Character>();                 teclado.put('1', '`');         teclado.put('2', '1');         teclado.put('3', '2');         teclado.put('4', '3');         teclado.put('5', '4');         teclado.put('6', '5');         teclado.put('7', '6');         teclado.put('8', '7');         teclado.put('9'

(UVA) 11479 - Is this the easiest problem? - 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 = "";         StringTokenizer tokenizer = null;                 String entrada = "";         long[] vetor = new long[3];                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                line = br.readLine();         tokenizer = new StringTokenizer(line);         int n = Integer.parseInt(tokenizer.nextToken());         int contador = 0;                 while (contador < n) {                    entrada = br.readLine();             String[] str = entrada.split(" ");             int i = 0;             while (i < 3) {               

(UVA) 10035 - Primary Arithmetic - 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();         Integer tamanho = line.length();                 int somaV1 = 0;         int somaV2 = 0;                 // Le vetor 1          ArrayList<Integer> vetor = new ArrayList<Integer>();                 Integer i = 0;         while (line.charAt(i) != ' ') {             vetor.add(i, line.charAt(i)-'0');             somaV1 += vetor.get(i);             i++;         }                 // Le vetor 2         ArrayList<Integer> v

(UVA) 10106 - Product - Solução

import java.io.*; import java.util.*; import java.lang.Math; import java.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));           Integer contador = 0;         BigInteger numero = new BigInteger("1");                 while ((line = br.readLine()) != null) {             BigInteger entrada = new BigInteger(line);             contador++;             if (contador <= 2) {                 numero = numero.multiply(entrada);                 if (contador == 2) {                     System.out.println(numero);                     numero = new BigInteger("1");                     contad

(UVA) 10071 - Back to High School Physics - 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 entrada = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 while ((entrada = br.readLine()) != null) {                    String[] str = entrada.split(" ");             Integer a = Integer.parseInt(str[0]);             Integer b = Integer.parseInt(str[1]);                         System.out.println((a*b)*2);         }                         return;     } }

(UVA) 10018 - Reverse and Add - 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 = "";         String entrada = "";         StringTokenizer tokenizer = null;                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 line = br.readLine();         tokenizer = new StringTokenizer(line);         Integer n = Integer.parseInt(tokenizer.nextToken());         Integer contador = 0;                 while (contador < n) {              entrada = br.readLine();                         Integer tamanho = entrada.length();             ArrayList<Integer> vetor = new ArrayList<Integer>();                         Integ

(UVA) 344 - Roman Digititis - Solução

import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.takeInput();                 System.exit(0);     }     List<Integer> geraQuantidade(int numeroEntrada) {         List<Integer> lista = new ArrayList<Integer>();             int n = 1;         int i = 0;         int v = 0;         int x = 0;         int l = 0;         int c = 0;         while (n <= numeroEntrada) {             int valor = n;                         i += verificaI(n);             v += verificaV(n);             x += verificaX(n);             if (n < 10) {                         }             else if (n >= 10 && n < 20) {                 x++;             }             else if (n >= 20 && n < 30) {                 x += 2;             }             else if (n >= 30 && n < 40) {  

(UVA) 10107 - What is the Median? - 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 = "";         StringTokenizer tokenizer = null;                 int[] vetor = new int[10000];         int contador = 0;                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 while ((line = br.readLine()) != null) {             tokenizer = new StringTokenizer(line);             int x = Integer.parseInt(tokenizer.nextToken());             vetor[contador] = x;                         contador++;                         Arrays.sort(vetor, 0, contador);                                     if (contador%2 == 0) {                 System.out.printl

(UVA) 12626 - I ❤ Pizza - Solução

import java.io.*; import java.util.*; 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 = "";         StringTokenizer tokenizer = null;                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 line = br.readLine();         tokenizer = new StringTokenizer(line);         int n = Integer.parseInt(tokenizer.nextToken());                 int contador = 0;         while (contador < n) {             int[] qte = new int[6];                         String[] vetor = new String[1];             vetor[0] = "";             vetor[0] = br.readLine();                                     for (int i = 0; i < vetor[0].length(); i++) {                 if (vet