(UVA) 10035 - Primary Arithmetic - Solução

import java.io.*;
import java.util.*;
import java.lang.Math ;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.takeInput();
       
        System.exit(0);
    }
   
    void takeInput() throws NumberFormatException, IOException {
        String line = "";
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        line = br.readLine();
        Integer tamanho = line.length();
       
        int somaV1 = 0;
        int somaV2 = 0;
       
        // Le vetor 1 
        ArrayList<Integer> vetor = new ArrayList<Integer>();
       
        Integer i = 0;
        while (line.charAt(i) != ' ') {
            vetor.add(i, line.charAt(i)-'0');
            somaV1 += vetor.get(i);
            i++;
        }
       
        // Le vetor 2
        ArrayList<Integer> vetor2 = new ArrayList<Integer>();
       
        i++;
        Integer j = 0;
        while (i < tamanho) {
            vetor2.add(j, line.charAt(i)-'0');
            somaV2 += vetor2.get(j);
            i++;
            j++;
        }
       
        while (somaV1 != 0 || somaV2 != 0) {    
            // Reposiciona o menor vetor, se tiverem tamanhos diferentes
            int tmp;          
            if (vetor.size() > vetor2.size()) {
                tamanho = vetor.size();
               
                tmp = vetor2.size();
                for (i = vetor2.size(); i < tamanho; i++) {
                    vetor2.add(i, 0);
                }
               
                j = tamanho-1;
                for (i = tmp-1; i >= 0; i--) {
                    vetor2.set(j, vetor2.get(i));
                    j--;
                }
                for (i = j; i >= 0; i--) {
                    vetor2.set(i, 0);
                }
            }
            else if (vetor.size() < vetor2.size()) {
                tamanho = vetor2.size();
           
                tmp = vetor.size();
                for (i = vetor.size(); i < tamanho; i++) {
                    vetor.add(i, 0);
                }
               
                j = tamanho-1;
                for (i = tmp-1; i >= 0; i--) {
                    vetor.set(j, vetor.get(i));
                    j--;
                }
                for (i = j; i >= 0; i--) {
                    vetor.set(i, 0);
                }
            }
     
            // Verifica quantos carrys existe
            int contaMaisUm = 0;
            boolean maisUm = false;
            int soma;
            ArrayList<Integer> vetorSoma = new ArrayList<Integer>();
            for (i = 0; i < vetor.size(); i++) {
                vetorSoma.add(i, 0);
            }
           
            for(i = vetor.size()-1; i >= 0; i--) {
                soma = vetor.get(i) + vetor2.get(i);
                vetorSoma.set(i, soma);
                if (maisUm == true) {
                    vetorSoma.set(i, vetorSoma.get(i) + 1);
                    maisUm = false;
                }
                if (vetorSoma.get(i) > 9 && i != 0) {
                    vetorSoma.set(i, vetorSoma.get(i) % 10);
                    maisUm = true;
                    contaMaisUm++;
                }
                else if (vetorSoma.get(i) > 9 && i == 0) {
                    contaMaisUm++;
                }                   
            }

 
            // Imprime mensagem final
            if (contaMaisUm == 0) {
                System.out.println("No carry operation.");
            }
            else if (contaMaisUm == 1) {
                System.out.println(contaMaisUm + " carry operation.");
            }
            else {
                System.out.println(contaMaisUm + " carry operations.");
            }

            // Limpa vetores
            vetor.clear();
            vetor2.clear();
                       
            line = br.readLine();
            tamanho = line.length();

            // Le novos vetores
            i = 0;
            somaV1 = 0;
            while (line.charAt(i) != ' ') {
                vetor.add(i, line.charAt(i)-'0');
                somaV1 += vetor.get(i);
                i++;
            }
           
            i++;
            j = 0;
            somaV2 = 0;
            while (i < tamanho) {
                vetor2.add(j, line.charAt(i)-'0');
                somaV2 += vetor2.get(j);
                i++;
                j++;
            }
        }
       
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução