Resoluções da etapa 6

Desafio 1 – Contando Passos

Enunciado
Resolução
import java.util.Scanner;

public class contandoPassos{

    public static boolean coordenadaValida(int i, int j){
        if(i < 0 || i >= 1000 || j < 0 || j >= 1000)
            return false;
        return true;
    }

    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int baixo = 0, esq = 0, dir = 0, cima = 0;
        int cAtualX = -1, cAtualY = -1, cAntX = 0, cAntY = 0;
        int dirX = 0, dirY = 0;

        for(int i = 0; i < n; i++){        
            cAtualX = in.nextInt();
            cAtualY = in.nextInt();
            
            if(coordenadaValida(cAtualX, cAtualY) && coordenadaValida(cAntX, cAntY)){
            dirX = cAtualX - cAntX;
            dirY = cAtualY - cAntY;
            if(dirX > 0 && dirY == 0)
                baixo++;
            if(dirX < 0 && dirY == 0)
                cima++;
            if(dirX == 0 && dirY < 0) 
                esq++;
            if(dirX == 0 && dirY > 0)
                dir++;

            cAntX = cAtualX;
            cAntY = cAtualY;
            }
        }

        System.out.print(
            baixo + " passo(s) para baixo\n" +
            esq + " passo(s) para esquerda\n" +
            dir + " passo(s) para direita\n" +
            cima + " passo(s) para cima\n"
        );
    }
}

Desafio 2 – Kaluanã, o mais novo nobre guerreiro

Enunciado
Resolução
import java.util.Scanner;

public class guerreiro {
    
    static boolean verificaCoordenada(int x, int y, int r){ //Como a coordenada do centro eh (0,0) nao precisamos inclui-la na equação da circunferencia
        if(x*x + y*y <= r*r)
            return true;
        return false;
    }

    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int x, y;

        for(int i = 0; i < n; i++){
            x = in.nextInt();
            y = in.nextInt();

            if(verificaCoordenada(x, y, 40))
                System.out.println("Faixa amarela! Voce sera o melhor cacador da aldeia!");
            else if(verificaCoordenada(x, y, 55))
                System.out.println("Faixa vermelha! Quase la, nao desista!");
            else if(verificaCoordenada(x, y, 70))
                System.out.println("Faixa azul! Por pouco!");
            else if(verificaCoordenada(x, y, 85))
                System.out.println("Faixa preta! Continue tentando!");
            else if(verificaCoordenada(x, y, 100))
                System.out.println("Faixa branca, preste atencao!!");
            else 
                System.out.println("Vish, passou longe...");
        }
    }
}

Desafio 3 – Caça Ao Jenipapo

Enunciado
Resolução
import java.util.Scanner;
public class Jenipapo {
    public static boolean jenipapo(int num){
        for(int j=2; j<=num; j++) if(num%j == 0 && num != j) return false;
        return true;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i=0; i<n; i++){
            int jenipapo = sc.nextInt();
            if(!jenipapo(jenipapo) || jenipapo <2) System.out.println("Viajou, Ipi? Aqui tem essa fruta nao");
            else{
                int j = 2, cont = 0;
                while(j <= jenipapo){
                    if(jenipapo(j)) cont++;
                    j++;
                }
                int andar = (int) (Math.log(cont) / Math.log(2));
                System.out.println("Ipi pegou jenipapo do galho "+andar);
            }
        }
    }
}

Desafio 4 – Cuidado com o MORCEGÃO!

Enunciado
Resolução
import java.util.Scanner;

public class CuidadoComOmorcegao {
    public static void horaDoVamoVer(int de3, int de5, String[] c){
        if(de5+de3 >=2) {
            int n=0,  o=0, m=0;
            while(n < 5*de5){
                System.out.print("[");
                while(m < 5) {
                    System.out.print(c[o]);
                    if(m<4) System.out.print(" ");
                    m++; o++;}
                System.out.print("] ");
                m=0;
                n += 5;
            }
            n=0;
            while(n < 3*de3){
                System.out.print("[");
                while(m < 3) {
                    System.out.print(c[o]); 
                    if(m<2) System.out.print(" ");
                    m++; o++;}
                System.out.print("] ");
                m=0;
                n += 3;
            }
            System.out.println("["+c[c.length-1]+"]");
        }
        else System.out.println("AAAAA o MORCEGAO atacou de novo!");
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        sc.nextLine();
        for(int j=0; j<num; j++){
            String a = sc.nextLine();
            String[] grupo = a.split(" ");  
            int resto = grupo.length;
            String aux = grupo[0];
            int n = 0;
            for(int i=0; i<grupo.length; i++){
                if(Integer.parseInt(aux) < Integer.parseInt(grupo[i])){
                    aux = grupo[i];
                    n = i;
                }    
            }
            grupo[n] = grupo[grupo.length-1];
            grupo[grupo.length-1] = aux;
            int de5=0, de3=0;
            while(resto>2){
                while(resto>4){
                    resto -= 5;
                    de5++; 
                }
                if(resto > 2){
                    resto -= 3;
                    de3++;
                } 
            }
            switch(resto){
                case 1:
                    horaDoVamoVer(de3, de5, grupo);
                break;
                default: 
                    while((resto-1) % 3 != 0  && de5>0){
                        resto += 5;
                        de5--;
                    }
                    if((resto-1) % 3 != 0)System.out.println("AAAAA o MORCEGAO atacou de novo!");
                    else{
                    int ajuda = resto-1;
                    resto -= ajuda;
                    de3 += (ajuda)/3;
                    horaDoVamoVer(de3, de5, grupo);
                    }
                break;
            }
        }
    }
}

Desafio 5 – Correndo Do Ao Ao

Enunciado
Resolução
import java.util.Scanner;
public class CorrendoDoAoAo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        sc.nextLine();
        for(int i=0; i<num; i++){
            
            String loc = sc.nextLine();
            String[] arrayloc = loc.split(" ");
            int l = Character.getNumericValue(arrayloc[0].charAt(1));
            int c = Character.getNumericValue(arrayloc[0].charAt(3));
            
            double endist = Double.POSITIVE_INFINITY;
            int linArvre=0, colArvre=0, distLin=0, distCol=0;
            for(int j=2; j<arrayloc.length; j++){
                int a = Character.getNumericValue(arrayloc[j].charAt(1));
                int b = Character.getNumericValue(arrayloc[j].charAt(3));
                double c1 = Math.pow(Math.abs(a-l)+1, 2);
                double c2 = Math.pow(Math.abs(b-c)+1, 2);
                double dist = Math.sqrt((c1+ c2));
                if(dist < endist) {
                    endist = dist;
                    linArvre = a; colArvre = b;
                    distLin = Math.abs(a-l);
                    distCol = Math.abs(b-c);
                }
            }
            if(endist >= Double.POSITIVE_INFINITY) System.out.println("AoAoAoAoVRAU!");
            else{
                System.out.print("Siga a ");
                double graus = 0;
                int medidas = Math.abs((distCol*2)- distLin); 
                double escala = 0;
                if(distCol >0){
                    if(distCol >= distLin) escala = 90.0/((distCol*2.0));
                    else{
                        escala = 90.0/((distLin*2.0));
                        medidas = distCol;
                    }
                }
                for(int n=0; n<medidas; n++) graus += escala;
                int grausint = (int) graus;
                String res = "";
                if(linArvre < l){ //casos N
                    res = Integer.toString(grausint)+"N : P = ("+distLin+" x N)";
                    if(colArvre < c) res = "-"+res+" + ("+distCol+" x O)";//noroeste
                    else if(colArvre > c) res = ""+res+" + ("+distCol+" x L)";//nordeste
                    System.out.println(res);
                }else if(linArvre > l) {//casos S
                    res = Integer.toString(grausint)+"S : P = ("+distLin+" x S)";
                    if(colArvre < c) res = ""+res+" + ("+distCol+" x O)";//sedoeste
                    else if(colArvre > c) res = "-"+res+" + ("+distCol+" x L)";//sudeste
                    System.out.println(res);
                }else{//casos L e O
                    if(colArvre < c) System.out.println("O : P = ("+distCol+" x O)"); //oeste 
                    else if(colArvre > c) System.out.println("L : P = ("+distCol+" x L)"); //leste
                }
            }
        }
    }
}

Desafio 6 – A Ave fantasma!

Enunciado
Resolução
import java.util.Scanner;

public class Urutau
{
    public static void main(String[] args)
    {
        //Criando o scanner
        Scanner sc = new Scanner(System.in);
        String atual = sc.nextLine();
        while(true)
        {
            if(atual.equals("XIU!")) break;
            System.out.println(contandoFoi(atual));
            atual = sc.nextLine();
        }
    }
    public static int contandoFoi(String s) {
        s = s.toLowerCase();
        boolean encontrouPalavra = false;
        int numDeO = 0;
        int resposta = 0;
        for(int i = 0; i<s.length(); i++) {
            if(encontrouPalavra) {
                if(s.charAt(i) == 'i') {
                    if(numDeO >= 1) {
                        resposta++;
                        numDeO = 0;
                        encontrouPalavra = false;
                    }
                    else {
                        encontrouPalavra = false;
                        numDeO = 0;
                    }
                }
                else if(s.charAt(i) == 'o') {
                    numDeO++;
                }
                else {
                    encontrouPalavra = false;
                    numDeO = 0;
                }
            }
            else if(s.charAt(i) == 'f') {
                encontrouPalavra = true;
            }
        }
        return resposta;
    }
}

Desafio 7 – Cálculo do Saci

Enunciado
Resolução
// A resposta da integral é -11/384 que é aproximadamente -0,0286
// Como o enunciado pediu o teto desse cálculo, então a resposta da 0
// Era possível calcular isso fora do código, usando um lápis ou uma calculadora
// Também é possível notar que é só fazer a + b pelos exemplos
// Porem os numeros estouram o int, entao vc deve usar double na resolucao

import java.util.Scanner;

public class Saci{
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);

		int n = input.nextInt();

		for(int i = 0; i < n; i++){
			long num1 = input.nextLong();
			long num2 = input.nextLong();

			long resp = num1 + num2;

			System.out.println(resp);
		}

	}
}

Desafio 8 – Sopa da Cuca

Enunciado
Resolução
import java.util.Scanner;

public class Cuca {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int casosDeTeste = sc.nextInt();
        sc.nextLine();
        for(int i = 0; i<casosDeTeste;i++) {
            int proporcaoAbacaxi = sc.nextInt();
            int proporcaoBanana = sc.nextInt();
            int proporcaoGoiaba = sc.nextInt();
            int proporcaoMaracuja = sc.nextInt();
            sc.nextLine();
            String fruteira = sc.nextLine();
            int[] frutas = contaLetras(fruteira);
            //Considera a menor quantidade que pode ser preparada
            int menor = 1000000000;

            //Verificando se a quantia de abacaxi eh zero, para nao fazer as contas
            if(proporcaoAbacaxi != 0)
            {
                menor = frutas[0]/proporcaoAbacaxi;
            }
            //Verificando se a quantia de banana eh zero, para nao fazer as contas
            if(proporcaoBanana != 0)
            {
                if( menor > frutas[1]/proporcaoBanana) menor = frutas[1]/proporcaoBanana;
            }
            //Verificando se a quantia de goiaba eh zero, para nao fazer as contas
            if(proporcaoGoiaba != 0)
            {
                if( menor > frutas[2]/proporcaoGoiaba) menor = frutas[2]/proporcaoGoiaba;
            }
            //Verificando se a quantia de maracuja eh zero, para nao fazer as contas
            if(proporcaoMaracuja != 0)
            {
                if( menor > frutas[3]/proporcaoMaracuja) menor = frutas[3]/proporcaoMaracuja;
            }
            System.out.println(menor);
        }
    }

    //Metodo que conta o tanto de frutas que tem em cada string
    //Posicao 0 = Abacaxi
    //Posicao 1 = Banana
    //Posicao 2 = Goiaba
    //Posicao 3 = Maracuja
    public static int[] contaLetras(String s) {
        int[] resposta = new int[4];
        for(int i = 0; i<s.length() ;i++){
            if(s.charAt(i) == 'a') resposta[0]++;
            else if(s.charAt(i) == 'b') resposta[1]++;
            else if(s.charAt(i) == 'g') resposta[2]++;
            else if(s.charAt(i) == 'm') resposta[3]++;
        }
        return resposta;
    }
}

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *