Cod sursa(job #2775872)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 17 septembrie 2021 18:38:32
Problema Text Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <chrono>
#include <fstream>
#include <string>

#define DEBUG 1

std::ifstream cin("../text.in");
std::ofstream cout("../text.out");

bool is_letter(char t) {
    return (t >= 'a' && t <= 'z') || (t >= 'A' && t <= 'Z');
}


void solve() {
    int words = 0;
    int letters = 0;
    bool new_word;
    std::string input_string;

    while (cin.peek() != EOF) {
        std::getline(cin, input_string);
        new_word = true;

        for (auto t: input_string) {
            int letter_count = is_letter(t);
            letters += letter_count;

            if (!letter_count) { // daca nu e litera
                new_word = true; // ne pregatim sa incepem cuvant nou
            } else if (new_word) { // data e cuvant nou
                ++words; // crestem cuvintele
                new_word = false; // si tinem minte
            }
        }
    }

    if (DEBUG) {
        cout << "[letters: ]"<< letters << '\n' << "[words]: " << words << "\n" << "[average]";
    }
    cout << letters / words;

}


int main() {
    auto start = std::chrono::high_resolution_clock::now();

    solve();

    auto stop = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
    std::cout << std::endl << "[time]:" << duration.count() << std::endl;
    return 0;
}