(URI) Figurinhas - Solução

Para este problema, é necessário calcular o Máximo Divisor Comum. O MDC entre os dois valores de entrada referentes à quantidade de figurinhas nos fornece o resultado esperado.

 
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;
    }

    int mdc(int a, int b) {
        while(b != 0) {
            int quociente = a/b;
            int resto = a%b;
            a = b;
            b = resto;
        }
       
        return a;
    }
       
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int qteCasos = leitor(br);
       
        for (int i = 0; i < qteCasos; i++) {
            int f1 = leitor(br);
            int f2 = leitor(br);
           
            int mdcResposta = 0;
            if (f1 > f2) {
                System.out.println(mdc(f1, f2));
            }
            else {
                System.out.println(mdc(f2, f1));
            }
           
        }
   }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução