Pagini recente » Cod sursa (job #1036568) | Cod sursa (job #2890503) | Profil MirunaStefania | Cod sursa (job #1839314) | Cod sursa (job #232406)
Cod sursa(job #232406)
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <algorithm>
#include <math.h>
#include <windows.h>
using namespace std;
long cmmdc(long a, long b)
{
return ( b == 0 ? a : cmmdc(b, a % b) );
}
int main()
{
//LARGE_INTEGER t1;
//QueryPerformanceCounter(&t1);
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 + 1; j < n; j++)
{
if (cmmdc(vec[i], vec[j]) == 1)
++nr;
}
}
cout << "nr=" << nr << endl;
outdata << nr;
outdata.close();
//LARGE_INTEGER t2;
//QueryPerformanceCounter(&t2);
//cout << "time=" << t2.QuadPart - t1.QuadPart << endl;
//cout << t1.HighPart << " " <<t1.LowPart <<" " << endl;
//cout << t2.HighPart << " " <<t2.LowPart <<" " << endl;
return 0;
}