(SPOJ) 19970 - Letras - 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);
    }
   
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        String line = br.readLine();
        char letra = line.charAt(0);

        boolean possuiLetraEspecial = false;
       
        String palavra = "";
        int palavraComLetra = 0;
       
        line = br.readLine(); // lê linha com a frase
        StringTokenizer tokenizer = new StringTokenizer(line);
       
        int contaTokens = tokenizer.countTokens();
        for (int j = 0; j < contaTokens; j++) {
            palavra = String.valueOf(tokenizer.nextToken());
            int tamPalavra = palavra.length();
            for (int i = 0; i < tamPalavra; i++) {
                if (palavra.charAt(i) == letra) {
                    possuiLetraEspecial = true;
                }
            }
            if (possuiLetraEspecial) {
                palavraComLetra++;
                possuiLetraEspecial = false;
            }
        }       
       
        float porcentagem;
        if (palavraComLetra == 0 || contaTokens == 0) {
            System.out.println("0.0");
        }
        else {
            float res = ((float)palavraComLetra/(float)contaTokens)*100;
            System.out.printf("%.1f\n", res);
        }
           
        return;
    }
   
}


Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução