A sequence of natural numbers is given. Find the largest common divisor of all the elements of the sequence and reduce all its members by it.
Input data
The first line of the input data contains a natural number n
(n⩽100
) is the number of elements in the sequence. The next line contains n
natural numbers not exceeding 10000
— the elements of the sequence.
Output data
Print n
numbers — the final sequence.
Example
Input
5
3 6 9 12 18
Conclusion
1 2 3 4 6
I have code, but I don’t know what these lines of code do:
for (auto z = 1; z < max; z++)
if (isDivisor(A, z)) x.push_back(z);
for (auto y : x)
if (nod < y) nod = y;
Full code:
#include <iostream>
#include <vector>
using namespace std;
bool isDivisor(vector<int> &A, int d)
{
for (auto c : A)
{
if (c % d != 0)
return false;
}
return true;
}
void reduction(vector<int> &A)
{
int nod = 1, max = 0;
vector <int> x;
for (int i = 0; i < A.size(); i++)
if (max < A[i]) max = A[i];
for (auto z = 1; z < max; z++)
if (isDivisor(A, z)) x.push_back(z);
for (auto y : x)
if (nod < y) nod = y;
for (int i = 0; i < A.size(); i++)
A[i] /= nod;
cout << endl;
}
{
for (auto c : A)
{
if (c % d != 0)
return false;
}
return true;
}
void reduction(vector<int> &A)
{
int nod = 1, max = 0;
vector <int> x;
for (int i = 0; i < A.size(); i++)
if (max < A[i]) max = A[i];
for (auto z = 1; z < max; z++)
if (isDivisor(A, z)) x.push_back(z);
for (auto y : x)
if (nod < y) nod = y;
for (int i = 0; i < A.size(); i++)
A[i] /= nod;
cout << endl;
}
int main()
{
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; ++i){
cin >> A[i];
}
reduction(A);
for (int i = 0; i < A.size(); ++i){
cout << A[i] << " ";
}
return 0;
}
Pls explain it to me.
Kirill Bure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.