Cod sursa(job #786026)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 10 septembrie 2012 13:22:14
Problema Algoritmul lui Euclid extins Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb

#include <iostream>
#include <fstream>

std::ifstream f("euclid3.in");
std::ofstream g("euclid3.out");

void euclid(int a, int b, int &d, int &x, int &y)
{
    if (b == 0) {
        x = 1;
        y = 0;
        d = a;
    }
    else {
        int x0, y0;
        euclid(b, a % b, d, x0, y0);

        y = x0 - (a / b) * y0;
        x = y0;
    }
}

int main()
{
    int noTests;
    int a, b, c, d, x, y;

    f >> noTests;

    for (int t = 0; t < noTests; ++t) {
        f >> a >> b >> c;

        euclid(a, b, d, x, y);

        if (c % d == 0) {
            g << (x * (c / d)) << ' ' << (y * (c / d)) << '\n';
        }
        else {
            g << "0 0\n";
        }
    }

    return 0;
}