(URI) Onde está o Mármore? - 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 leitor(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));
       
        int qteMarmores = leitor(br);
        int qteConsultas = leitor(br);
       
        int contador = 0;
        while (qteMarmores != 0 || qteConsultas != 0) {
            contador++;
            System.out.println("CASE# "+ contador +":");
           
            int[] marmores = new int[qteMarmores];
           
            for (int i = 0; i < qteMarmores; i++) {
                marmores[i] = leitor(br);
            }
           
            Arrays.sort(marmores);
           
            int consulta;
            for (int i = 0; i < qteConsultas; i++) {
                consulta = leitor(br);
                boolean encontrou = false;
               
                for (int j = 0; j < qteMarmores; j++) {                   
                    if (consulta == marmores[j]) {
                        System.out.println(consulta + " found at " + (j+1));
                        encontrou = true;
                       
                        break;
                    }
                }
                if (!encontrou) {
                    System.out.println(consulta + " not found");
                }
            }
           
            qteMarmores = leitor(br);
            qteConsultas = leitor(br);
        }
   }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução