Pagini recente » Cod sursa (job #1811531) | Cod sursa (job #1133756) | Cod sursa (job #496953) | Cod sursa (job #1497505) | Cod sursa (job #232394)
Cod sursa(job #232394)
#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;
}