(UVA) 10018 - Reverse and Add - 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 = "";
        String entrada = "";
        StringTokenizer tokenizer = null;
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        line = br.readLine();
        tokenizer = new StringTokenizer(line);
        Integer n = Integer.parseInt(tokenizer.nextToken());
        Integer contador = 0;
       
        while (contador < n) { 
            entrada = br.readLine();
           
            Integer tamanho = entrada.length();
            ArrayList<Integer> vetor = new ArrayList<Integer>();
           
            Integer i;
            for (i = 0; i < tamanho; i++) {
                vetor.add(i, entrada.charAt(i)-'0');
            }
           
            Integer contaIt = 0;      
            boolean palindromo = false;
            while (palindromo == false) {
                contaIt++;         
                ArrayList<Integer> vetor2 = new ArrayList<Integer>();
               
                Integer j = 0;
                for (i = tamanho-1; i >= 0; i--) {
                    vetor2.add(j, vetor.get(i));
                    j++;
                }
               
               
                boolean maisUm = false;
                boolean reposiciona = false;
                int soma;
                ArrayList<Integer> vetorSoma = new ArrayList<Integer>();
                for (i = 0; i < tamanho; i++) {
                    vetorSoma.add(i, 0);
                }
               
                for(i = tamanho-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;
                    }
                    else if (vetorSoma.get(i) > 9 && i == 0) {
                        reposiciona = true;
                    }
                   
                }
               
                if (reposiciona == true) {
                    vetorSoma.add(tamanho, 0);
                    for (i = tamanho; i >= 1; i--) {
                        if (i != 1) {
                            vetorSoma.set(i, vetorSoma.get(i-1));
                        }
                        else { // i == 1
                            vetorSoma.set(i, vetorSoma.get(i-1)%10);
                            vetorSoma.set(i-1, 1);
                        }
                    }
                    tamanho += 1;
                }
               
                Integer contadorIgual = 0;
                for (i = 0; i < tamanho/2; i++) {
                    if (vetorSoma.get(i) == vetorSoma.get((tamanho-1)-i)) {
                        contadorIgual++;
                    }
                }

                if (contadorIgual == tamanho/2) {
                    palindromo = true;
                    System.out.print(contaIt + " ");
                    for (i = 0; i < tamanho; i++) {
                        System.out.print(vetorSoma.get(i));
                    }
                    System.out.println();
                }
                else {
                    if (reposiciona == false) {
                        for (i = 0; i < tamanho; i++) {
                            vetor.set(i, vetorSoma.get(i));
                        }
                    }
                    else {
                        vetor.add(tamanho-1, 0);
                        for (i = 0; i < tamanho; i++) {
                            vetor.set(i, vetorSoma.get(i));
                        }
                    }
                }
            }
           
            contador++;
        }
       
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução