Cod sursa(job #1497229)

Utilizator DonleenoVass Arnold Donleeno Data 6 octombrie 2015 14:56:33
Problema Algoritmul lui Euclid Scor 60
Compilator c Status done
Runda Arhiva educationala Marime 0.63 kb
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

long gcd(long a, long b){
	return b == 0 ? a : gcd(b, a%b);
}
long gcdGreaterThanZero(long a, long b){
	return a == b ? a : (a > b ? gcdGreaterThanZero(a - b, b) : gcdGreaterThanZero(a, b - a));
}

int main(){
	FILE *input = fopen("euclid2.in","r");
	FILE *output = fopen("euclid2.out","w");

	int n;
	long a, b;
	fscanf(input, "%d", &n);

	for (int i = 0; i < n; i++)
	{
		fscanf(input, "%ld %ld", &a, &b);
		if (a  >0 && b > 0){
			fprintf(output, "%ld\n", gcdGreaterThanZero(a, b));
		}
		else
		{
			fprintf(output, "%ld\n", gcd(a, b));
		}
	}
	return 0;
}