Posts

Sites com problemas computacionais

Veja abaixo alguns links onde é possível treinar a resolução de problemas de programação: - SPOJ: http://br.spoj.com/ - UVA: http://uva.onlinejudge.org/ - Coderbyte: http://coderbyte.com/ - URI: http://www.urionlinejudge.com.br/

Selection Sort

É um algoritmo de ordenação simples, o qual verifica qual é o menor elemento no vetor e o coloca na primeira posição. Em seguida, verifica qual é o segundo menor elemento e o coloca na segunda posição, e assim sucessivamente até os últimos elementos. import java.io.*; import java.util.*; class Main {     public static void main(String[] args) throws NumberFormatException, IOException {         Main ordenacao = new Main();         ordenacao.ordena();                System.exit(0);     }         static int leitor(BufferedReader br) throws NumberFormatException, IOException {         int n;         int resp = 0;         int sinal = 1;         while (true) {...

(Coderbyte) Prime Mover - Solução

Using the Java language, have the function PrimeMover( num ) return the num th prime number. The range will be from 1 to 10^4. For example: if num is 16 the output should be 53 as 53 is the 16th prime number. Solução: import java.util.*; import java.io.*; class Function {    int PrimeMover(int num) {     int numeroRequerido = 2;     int contador = 1; // possuo 1 número primo     int indice = 3;     boolean primo = true;     while (contador < num) {       for (int i = 2; i < indice; i++) {         if (indice%i == 0) {           primo = false;           break;         }       }             if (primo) {     ...

(Coderbyte) Palindrome Two - Solução

Have the function PalindromeTwo( str ) take the str parameter being passed and return the string true if the parameter is a palindrome, (the string is the same forward as it is backward) otherwise return the string false . The parameter entered may have punctuation and symbols but they should not affect whether the string is in fact a palindrome. For example: "Anne, I vote more cars race Rome-to-Vienna" should return true . Solução: import java.util.*; import java.io.*; class Function {    String PalindromeTwo(String str) {       str = str.toLowerCase();         String apenasLetras = "";     for (int i = 0; i < str.length(); i++) {       if (Character.isLetter(str.charAt(i))) {         apenasLetras += str.charAt(i);       }     }         int tam = apenasLetras.length(...

(Coderbyte) Prime Time - Solução

Using the Java language, have the function PrimeTime( num ) take the num parameter being passed and return the string true if the parameter is a prime number, otherwise return the string false . The range will be between 1 and 2^16. Solução: import java.util.*; import java.io.*; class Function {    String PrimeTime(int num) {     for (int i = 2; i < num; i++) {       if (num%i == 0) {         return "false";       }     }            return "true";       }     public static void main (String[] args) {      // keep this function call here         Scanner  s = new Scanner(System.in);     Function c = new Function();     System.out.print(c.PrimeTime(s.nextLine()));   ...

(Coderbyte) Counting Minutes I - Solução

Have the function CountingMinutesI( str ) take the str parameter being passed which will be two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:00am-10:00am then the output should be 60 . If str is 1:00pm-11:00am the output should be 1320 .  Solução: import java.util.*; import java.io.*; import java.lang.*; class Function {   int CountingMinutesI(String str) {       String[] horas = str.split("-");        int minutosIniciais = geraMinutos(horas[0]);     int minutosFinais = geraMinutos(horas[1]);        int resposta = 0;     if (minutosIniciais < minutosFinais) {          resposta = minutosFinais - minutosIniciais;     }     else { ...

(Coderbyte) Third Greatest - Solução

Have the function ThirdGreatest( strArr ) take the array of strings stored in strArr and return the third largest word within in. So for example: if strArr is ["hello", "world", "before", "all"] your output should be world because "before" is 6 letters long, and "hello" and "world" are both 5, but the output should be world because it appeared as the last 5 letter word in the array. If strArr was ["hello", "world", "after", "all"] the output should be after because the first three words are all 5 letters long, so return the last one. The array will have at least three strings and each string will only contain letters.  Solução: import java.util.*; import java.io.*; class Function {    String ThirdGreatest(String[] strArr) {     int[] contaCaracteres = new int[strArr.length];         for (int i = 0; i < strArr.length; i++) {      contaCaracteres[i] = strArr...