Cod sursa(job #232394)

Utilizator hello2alex gen2 hello2 Data 15 decembrie 2008 10:10:42
Problema Pairs Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <algorithm>
#include <math.h>

using namespace std;

long cmmdc(long a, long b)
{
	if (a == b) return a;
	if (a > b)
		return cmmdc (b, a-b);
	else
		return cmmdc(a, b-a);
}

int main()
{
	ifstream indata; // indata is like cin
	ofstream outdata;
	indata.open("pairs.in"); // opens the file
	outdata.open("pairs.out");
	if(!indata) { // file couldn't be opened
		cerr << "Error: file could not be opened" << endl;
		exit(1);
	}
	long n;
	long* vec;
	indata >> n;
	vec = new long[n];
	cout << "n=" << n << endl;
	for (long i = 0; i < n; i++)
	{
		indata >> vec[i];
		cout << vec[i] << " ";
	}
	cout << endl;
	indata.close();
	long nr = 0;
	for (long i = 0; i < n; i++)
	{
		for (long j = i; j < n; j++)
		{
			if (cmmdc(vec[i], vec[j]) == 1)
				nr++;
		}
	}
	cout << "nr=" << nr << endl;
	outdata << nr;
	outdata.close();
	return 0;
}