(SPOJ) 2846 - Paridade - 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 gambis(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 {
        int valor;
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
             
        while((valor = gambis(br)) != 0) {
            int soma = 0;
            String binario = Integer.toString(valor, 2);
           
            for (int i = 0; i < binario.length(); i++) {
                soma += binario.charAt(i)-'0';
            }

            bw.write("The parity of " + binario + " is " + soma + " (mod 2).\n");
        }

        bw.flush();
        bw.close();                   
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução