Posts

Showing posts from January, 2014

(SPOJ) 3742 - Feynman - 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 valor = br.readLine();         int vInt = Integer.parseInt(valor);         while(vInt != 0) {             int resultado = (vInt*(vInt+1)*(vInt+vInt+1))/6;             bw.write(resultado+"\n");             valor = br.readLine();             vInt = Integer.parseInt(valor);         }                 bw.flush();         bw.close();         return;     } }

(SPOJ) 3087 - O Fantástico Jaspion! - 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 = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);         int qteInstancias = Integer.parseInt(tokenizer.nextToken());                     for (int i = 0; i < qteInstancias; i++) {             line = br.readLine();             tokenizer = new StringTokenizer(line);             int qtePares = Integer.parseInt(tokenizer.nextToken());             int linhasMusica = Integer.parseInt(tokenizer.nextToken());                         Map<String, String> tr

(SPOJ) 3094 - Elementar, meu caro Watson! - Solução 2

Solução mais eficiente 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 gambis(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) 3094 - Elementar, meu caro Watson! - Solução 1

Solução menos eficiente 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 gambis(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) 2844 - Você pode dizer 11 - 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));                 while((line = br.readLine()) != null) {             if (line.length() == 1 && line.charAt(0) == '0') {                 return;             }                         int pares = 0;             int impares = 0;             for (int i = 0; i < line.length(); i=i+2) {                 pares += (line.charAt(i)-'0');             }             for (int i = 1; i < line.length(); i=i+2) {                 imp

(SPOJ) 1745 - Recuperação - 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 gambis(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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamRe

(SPOJ) 2284 - Palavras primas - 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));                       int[] vetor = new int[60];                 vetor['a'-'A'] = 1;         vetor['b'-'A'] = 2;         vetor['c'-'A'] = 3;         vetor['d'-'A'] = 4;         vetor['e'-'A'] = 5;         vetor['f'-'A'] = 6;         vetor['g'-'A'] = 7;         vetor['h'-'A'] = 8;         vetor['i'-'A'] = 9;         vetor['j&#

(SPOJ) 2846 - Paridade - 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 gambis(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 {         int valor;                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         BufferedWriter bw = new BufferedWriter(new Outp

(SPOJ) 2280 - Encontre o telefone - Solução 2

Solução menos eficiente, pois usa o Map . 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 {         Map<Character, Character> alfabeto = new TreeMap<Character, Character>();                 alfabeto.put('A', '2');         alfabeto.put('B', '2');         alfabeto.put('C', '2');         alfabeto.put('D', '3');         alfabeto.put('E', '3');         alfabeto.put('F', '3');         alfabeto.put('G', '4');         alfabeto.put('H', '4');         alfabeto.put('I', '4');         alfabeto.put('J', '5');         alfabeto.put('K', '5');         alf

(SPOJ) 2280 - Encontre o telefone - Solução 1

Solução mais eficiente. 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 {         int[] vetor = new int[91];                 vetor[(int)'A'] = '2';         vetor[(int)'B'] = '2';         vetor[(int)'C'] = '2';         vetor[(int)'D'] = '3';         vetor[(int)'E'] = '3';         vetor[(int)'F'] = '3';         vetor[(int)'G'] = '4';         vetor[(int)'H'] = '4';         vetor[(int)'I'] = '4';         vetor[(int)'J'] = '5';         vetor[(int)'K'] = '5';         vetor[(int)'L'] = '5';         vetor[(int)'M&

(SPOJ) 2839 - Popularidade - 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 gambis(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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         int qteAlunos = gambis(br);   

(SPOJ) 1831 - f91 - Solução

Solução em C, pois a solução em Java excedia o limite de tempo. #include<stdio.h> int main() {     int valor, resultado;     while ((scanf("%d",&valor) == 1) && (valor != 0)) {         if (valor > 100) {             resultado = valor - 10;         }         else {             resultado = 91;         }         printf("f91(%d) = %d\n", valor, resultado);     }     return 0; }

(UVA) 10696 - f91 - 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 gambis(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));         BufferedWriter bw = new BufferedWriter(new

(SPOJ) 11651 - Banda - Solução 2

Image
Solução eficiente import java.io.*; import java.util.*; class Main {     class No {         public int no1;         public int no2;         public int no3;         public int afinidade;     }     public static void main(String[] args) throws NumberFormatException, IOException {         Main processando = new Main();         processando.processa();         System.exit(0);     }     int pos;     int leproximo(String line) {         int n = 0;         while (line.charAt(pos) == ' ') {             pos++;         }         for ( ; pos < line.length() && line.charAt(pos) != ' '; pos++) {             n = n*10 + line.charAt(pos) - '0';         }         return n;     }     void processa() throws NumberFormatException, IOException {         String line = "";         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         line = br.readLine();         StringTokenizer tokenizer = new Stri

(SPOJ) 11651 - Banda - Solução 1

1ª Solução tentada: não eficiente. Não é necessário o vetor, uma vez que a última afinidade é a maior. Com isso também não é necessário o Array.sort(). É possível tratar diferente os casos especiais, sem a necessidade de vários ifs. import java.io.*; import java.util.*; class Main {     class No {         public int no1;         public int no2;         public int no3;         public int afinidade;     }         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 qteMusicos = Integer.valueOf(tokenizer.nextToken());         int qtePares = Integer.valueO

(SPOJ) 11001 - Corrida - Solução

import java.io.*; import java.util.*; class Main {     class Competidor {         public int idt;         public long tempoFinal;     }         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 qteCarros = Integer.valueOf(tokenizer.nextToken());         int qteVoltas = Integer.valueOf(tokenizer.nextToken());                 Competidor[] competidores = new Competidor[qteCarros];                 for (int i = 0; i < qteCarros; i++) {             competidores[i] = new Competidor();         }                 long menor = 10

(SPOJ) 10865 - 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);     }         void processa() throws NumberFormatException, IOException {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         line = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);         int qteCompetidores = Integer.valueOf(tokenizer.nextToken());         int qteVoltas = Integer.valueOf(tokenizer.nextToken());                 long menor = 1000000000L;         long soma = 0L;         int competidor = 0;         for (int i = 0; i < qteCompetidores; i++) {             soma = 0;             line = br.readLine();             tokenizer = new StringTokenizer(line);             for (int j = 0; j < qteVoltas; j++) {                 int tem

(SPOJ) 10867 - Calculadora - 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         line = br.readLine();         int qteLinhas = Integer.valueOf(line);                 line = br.readLine();         double resultado = 0.0;         int valor = line.charAt(0)-'0';         char op = line.charAt(2);         if (op == '*') {             resultado = 1.0*valor;         }         else {             resultado = 1.0/valor;                        }                 for (int i = 1; i < qteLinhas; i++) {             line = br.readLine();             valor = line.charAt(0)-'0';             op = line.charAt(2);      

(SPOJ) 10866 - Transporte - 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 {         String line = "";                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         line = br.readLine();         StringTokenizer tokenizer = new StringTokenizer(line);         int a = Integer.valueOf(tokenizer.nextToken());         int b = Integer.valueOf(tokenizer.nextToken());         int c = Integer.valueOf(tokenizer.nextToken());                    line = br.readLine();         tokenizer = new StringTokenizer(line);         int x = Integer.valueOf(tokenizer.nextToken());         int y = Integer.valueOf(tokenizer.nextToken());         int z = Integer.valueOf(tokenizer.nextToken());         long x

(SPOJ) 819 - Pedágio - 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);     }         int[] busca(int[][] mapa, int pontoPartidaL, int numVertices) {         int[] vetorVisitados = new int[numVertices];                         for (int i = 0; i < numVertices; i++) {             vetorVisitados[i] = 0;         }                 ArrayList<Integer> filaVisitados = new ArrayList<Integer>();         filaVisitados.add(pontoPartidaL);                 int verticeAtual = 0;                 int inicioFila = 0;         while (filaVisitados.size() != inicioFila) {             verticeAtual = filaVisitados.get(inicioFila);                         for (int tentar = 0; tentar < numVertices; tentar++) {                 if (vetorVisitados[tentar] == 0 && mapa[verticeAtual][tentar] == 1) {                     vetor