Cod sursa(job #2742405)

Utilizator andreilicaAndrei Lica Eduard andreilica Data 20 aprilie 2021 21:26:37
Problema Algoritmul lui Euclid Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <vector>;


int greatestCommonDivisor(const int& a, const int& b) {
    int maxValue{ std::max(a, b) };
    int minValue{ std::min(a, b) };
    int remainder{ maxValue % minValue };

    if (!remainder) {
        return minValue;
    }

    return greatestCommonDivisor(remainder, minValue);
}

int main()
{
    int numPairs{};
    std::vector<std::pair<int, int>> pairs;

    std::ifstream inFile{ "euclid2.in" };
    std::ofstream outFile{ "euclid2.out" };

    if (!outFile || !inFile) {
        return -1;
    }

    inFile >> numPairs;
    pairs.reserve(numPairs);

    for (int x1, x2; inFile >> x1 >> x2;) {
        pairs.push_back(std::make_pair(x1, x2));
    }

    for (const auto& pair : pairs) {
        outFile << greatestCommonDivisor(pair.first, pair.second) << '\n';
    }
}