PDA

View Full Version : [PBINFO] Problema #913



~Wolf~
03-12-2020, 12:51 PM
Link site: pbinfo (<b>You have to register to ba able to see this link</b>)
Link problema: inlocuire3 (<b>You have to register to ba able to see this link</b>)

#include <iostream>
using namespace std;
void citire(unsigned int& n, unsigned int v[])
{
cin >> n;
for (unsigned int i = 0; i < n; i++)
{
cin >> v[i];
}
}
bool prim(unsigned int x)
{
if (x <= 1)
return false;
if (x == 2)
return true;
if (x % 2 == 0)
return false;
for (unsigned int d = 3; d * d <= x; d += 2)
{
if (x % d == 0)
{
return false;
}
}
return true;
}
unsigned int urmatorul_prim(unsigned int p)
{
//e interesanta metoda, am invatat-o si eu de la profesor caci eu ma duceam pana la 1.000.000.000 cu un for..amuzant
//cat timp numarul nu este prim il vom creste cu 1, iar cand e prim ne oprim si il returnam
while (prim(p) == 0)
{
p++;
}
return p;
}
void inloc(unsigned int n, unsigned int v[])
{
for (unsigned int i = 0; i < n; i++)
{
//daca e prim, cautam urmatorul nr prim mai mare decat cel curent(asa cum cere in cerinta) si inlocuim in valoarea curenta
//daca nu trecem peste element(am pus acel else in principal fiindca dadea un warning(non-void)
if (prim(v[i]) == false)
{
v[i] = urmatorul_prim(v[i]);
}
else
{
continue;
}
}
}
void afisare(unsigned int n, unsigned int v[])
{
for (unsigned int i = 0; i < n; i++)
{
cout << v[i] << ' ';
}
}
int main()
{
unsigned int n, v[1001];
citire(n, v);
inloc(n, v);
afisare(n, v);
return 0;
}