(UVA) 11332 - Summing Digits - 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 resultado = "";
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               
        while ((resultado = br.readLine()) != null) {
            if (resultado.charAt(0)-'0' == 0) {
                return;
            }
             
            Integer tamanho = resultado.length();

            Integer soma;                     
            Integer i;
            do {
                soma = 0;
               
                for (i = 0; i < tamanho; i++) {
                    soma += (resultado.charAt(i)-'0');
                }

                resultado = String.valueOf(soma);
                tamanho = resultado.length();               
            } while (tamanho > 1);
           
            System.out.println(soma);
        }
       
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução