(SPOJ) 2285 - Krakóvia - Solução

import java.io.*;
import java.util.*;
import java.math.*;

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

    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String line = "";

        int qteItens = leitor(br);
        int qtePessoas = leitor(br);
       
        BigInteger soma;
        BigInteger divisao;

        int contador = 1;
        while (qteItens != 0 && qtePessoas != 0) {
            soma = BigInteger.ZERO;

            for (int i = 0; i < qteItens; i++) {
                line = br.readLine();
                soma = soma.add(new BigInteger(line));
            }
            divisao = soma.divide(BigInteger.valueOf(qtePessoas));
            bw.write("Bill #" + contador + " costs " + soma + ": each friend should pay " + divisao + "\n");

            qteItens = leitor(br);
            qtePessoas = leitor(br);

            if (qteItens != 0 && qtePessoas != 0) {
                bw.write("\n");
            }
            contador++;
        }
       
        bw.flush();
        bw.close();          

        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução