Desafio 1 – Princesa e o Maquech
Enunciado
Resolução
import java.util.Scanner; public class Resposta{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0; i <n; i++){ int d = sc.nextInt(); // deslocamento sc.nextLine(); String f = sc.nextLine().toLowerCase(); String resposta = ""; for(int j=0; j< f.length(); j++){ if(f.charAt(j) != ' '){ if(f.charAt(j) - d < 97) resposta += (char) (f.charAt(j) - d + 26); else resposta += (char)( f.charAt(j) - d); } else resposta += f.charAt(j); } System.out.println(resposta); } } }
Desafio 2 – O caminho para El Dorado
Enunciado
Resolução
import java.util.Scanner; public class ElDorado { public static void main(String[] args){ Scanner input = new Scanner(System.in); while(true){ String entrada = input.nextLine(); if(entrada.equals("")) System.exit(0); String[] nomes = entrada.split(" "); if(saoDeuses(nomes[0], nomes[1])) System.out.println("Todos saudam os deuses."); else System.out.println("Eles sao forasteiros, joguem no vulcao!!"); } } public static boolean saoDeuses(String s1, String s2){ if(s1.equalsIgnoreCase(s2)) return false; s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); return isFirstEqualsLast(s1) && isFirstEqualsLast(s2) && isLengthOdd(s1) && isLengthOdd(s2) && aVogal(s1) && aVogal(s2); } public static boolean isFirstEqualsLast(String s){ return s.charAt(0) == s.charAt(s.length() - 1); } public static boolean isLengthOdd(String s){ return s.length() % 2 != 0; } public static boolean aVogal(String s){ for(int i = 0; i < s.length(); i++) if(s.charAt(i) == 'a') if(checkVogal(s, i)) return true; return false; } public static boolean checkVogal(String s, int n){ String vogals = "eiou"; if(n > 0) if(vogals.contains("" + s.charAt(n-1))) return true; if(n < s.length()-1) if(vogals.contains("" + s.charAt(n+1))) return true; return false; } }
Desafio 3 – A festa em Cuzco
Enunciado
Resolução
import java.util.Scanner; public class FestaCuzco { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String nome = sc.nextLine(); while(!nome.equals("Acabaram os convidados")){ String[] nomeSeparado = nome.split(" "); if(nomeSeparado[0].equals("Inka")){ System.out.println("Aproveite a festa, Vossa Majestade."); } else if(nomeSeparado[0].equals("Comandante") || nomeSeparado[0].equals("General")||nomeSeparado[0].equals("Major")){ System.out.println("* Continencia *. Sim Senhor! Entrada Permitida."); } else if((nome.indexOf("Coletor de Impostos") != -1) && (nome.indexOf("Coletor de Impostos") == nome.lastIndexOf("Coletor de Impostos")) && (nome.indexOf("Coletor de Impostos") == 0 || nome.indexOf("Coletor de Impostos") == (nome.length() - "Coletor de Impostos".length()))){ System.out.println("Deixe seus tributos no balcao e seja bem vindo."); } else{ System.out.println("Pegue sua Lhama e vaza daqui!"); } nome = sc.nextLine(); } } }
Desafio 4 – Conquistando com os Incas
Enunciado
Resolução
import java.util.Scanner; public class conquistandoincas { public static void main(String[] args){ Scanner sc = new Scanner(System.in); boolean conquistou = false; int totalPopulacao = sc.nextInt(); sc.nextLine(); int totalExercito = (int)Math.floor(totalPopulacao/2); String instruc = sc.nextLine(); while (!instruc.equals("Bora Comemora!")) { String[] instrucSep = instruc.split(" "); int totExercInimigo = (int) Math.floor((Integer.parseInt(instrucSep[1]))/2); int porcentagem = (int)Math.floor((totExercInimigo * 100)/totalExercito); if(porcentagem <= 10){ //Sem Batalha conquistou = true; totalExercito += totExercInimigo; System.out.println(instrucSep[0] + " aceitou o acordo e agora faz parte do nosso Imperio!"); }else if(porcentagem <= 70){ //Com Batatlha conquistou = true; totalExercito += (int )Math.floor(totExercInimigo/2); System.out.println("Apos uma grande batalha, vencemos de "+instrucSep[0] + ", e eles se uniram ao nosso Imperio!"); }else{ //Não possivel ganhar System.out.println("Quale Comandante, melhor meter o pe..."); } instruc = sc.nextLine(); } System.out.println("Nosso Imperio agora tem " + totalExercito + " soldados!"); if(conquistou){ System.out.println("Que Mama-Quilla abencoe as novas terras e seu plantio!"); } } }
Desafio 5 – Sacrifício asteca
Enunciado
Resolução
import java.util.Scanner; public class Sacrificio{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); for(int i=0; i<n; i++){ String[] datas = sc.nextLine().split(" "); String[] data1 = datas[0].split("/"); String[] data2 = datas[1].split("/"); int dia1 = Integer.parseInt(data1[0]); int mes1 = Integer.parseInt(data1[1]); int ano1 = Integer.parseInt(data1[2]); int dia2 = Integer.parseInt(data2[0]); int mes2 = Integer.parseInt(data2[1]); int ano2 = Integer.parseInt(data2[2]); int total = (ano2-ano1)*360 + (mes2-mes1)*20 + (dia2-dia1); if(total < 0) total *= -1; if(total > 300) System.out.printf("Oh nao, atrasei %d dias o sacrificio, o deus Huitzilopochtli vai ficar pistola D:\n", total-300); else System.out.println("O universo esta perfeitamente balanceado, como todas as coisas devem ser"); } } }
Desafio 6 – Roubando no Placar
Enunciado
Resolução
import java.util.Scanner; public class Placar{ public static void main(String[] args){ Scanner input = new Scanner(System.in); while(true){ int tempo = input.nextInt(); if(tempo == 0) System.exit(0); int[][] placar = new int[tempo][4]; for(int i = 0; i < tempo; i++) for(int j = 0; j < 4; j++) placar[i][j] = input.nextInt(); trocarPlacar(placar); trocarPlacar(placar); int[] r = resultado(placar); System.out.printf("Real Madeira %d X %d Gods United\n", r[0], r[1]); if(r[0] > r[1]) System.out.println("Hoje tem churrasco rapaziada!!"); else if(r[0] < r[1]) System.out.println("Vamos virar carvao :("); else System.out.println("Nada acontece, feijoada."); } } public static void trocarPlacar(int[][] placar){ int[] troca = posMaiorDiferenca(placar); int temp = placar[troca[0]][troca[1]]; placar[troca[0]][troca[1]] = placar[troca[0]][troca[2]]; placar[troca[0]][troca[2]] = temp; } public static int[] resultado(int[][] m){ int[] r = new int[2]; for(int i = 0; i < m.length; i++){ for(int j = 0; j < 2; j++) r[0] += m[i][j]; for(int j = 2; j < 4; j++) r[1] += m[i][j]; } return r; } public static int[] posMaiorDiferenca(int[][] m){ // 0 -> linha / 1 -> coluna do madeira / 2 -> coluna do gods int[] pos = new int[3]; int dif = 0; for(int i = 0; i < m.length; i++) for(int j = 0; j < 2; j++) for(int k = m[0].length - 1; k > 1; k--) if(m[i][k] - m[i][j] > dif){ dif = m[i][k] - m[i][j]; pos[0] = i; pos[1] = j; pos[2] = k; } return pos; } }