Pagini recente » Cod sursa (job #297012) | Cod sursa (job #1286290) | Cod sursa (job #3197473) | Cod sursa (job #475218) | Cod sursa (job #232402)
Cod sursa(job #232402)
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <algorithm>
#include <math.h>
using namespace std;
long cmmdc(long a, long b)
{
return ( b == 0 ? a : cmmdc(b, a % b) );
}
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;
indata >> n;
long* 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;
}