Cod sursa(job #3212447)

Utilizator andrei_botorogeanuBotorogeanu Andrei andrei_botorogeanu Data 11 martie 2024 19:04:27
Problema Algoritmul lui Euclid Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include<iostream>
#include<fstream>
using namespace std;

typedef unsigned int uint;
typedef unsigned long ulong;

namespace Recursive {
	ulong gcd(ulong first, ulong second) {
		if(second == 0)
			return first;
		else 
			return Recursive::gcd(second, first%second);			
	}
}

namespace Iterative {
	ulong gcd(ulong first, ulong second) {
		ulong temp;
		while(second != 0) {
			temp = second;
			second = first % second;
			first = temp;
		}
		return first;
	}
}

int main() 
{
	const char * inFile = "euclid2.in";
	const char * outFile = "euclid2.out";

	ifstream fin(inFile);
	ofstream fout(outFile);
	if(!fin || !fout) {
		cout<<"Error opening files!"<<endl;
		return -1;
	}	
	
	ulong nPairs;
	ulong first, second;

	fin >> nPairs;
	for(ulong i = 0; i < nPairs; i++) {
		fin>>first;
		fin>>second;
		fout<< Iterative::gcd(first, second) << "\n";
	}

	fout.close();
	fin.close();
}