Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void citeste(int& n, int a[])
- {
- cin >> n;
- for (int i = 0; i < n; i++)
- cin >> a[i];
- }
- int prim(int x)
- {
- if ((x % 2 == 0 && x != 2) || x == 1)
- return 0;
- for (int i = 3; i * i <= x; i += 2)
- if (x % i == 0)
- return 0;
- return 1;
- }
- int det(int x)
- {
- if (x <= 2)
- return 2;
- int p = 0;
- if (prim(x))
- return x;
- for (int nr = x - 1; nr >= 2 && !p; nr--)
- {
- if (prim(nr))
- p = nr;
- }
- for (int nr = x + 1; ; nr++)
- {
- if (prim(nr))
- {
- if (x - p < nr - x)
- return p;
- return nr;
- }
- }
- }
- void Scrie(int n, int a[])
- {
- for (int i = 0; i < n; i++)
- cout << a[i] << ' ';
- }
- #define NMAX 100
- int a[NMAX], n;
- int main()
- {
- citeste(n, a);
- for (int i = 0; i < n; i++)
- a[i] = det(a[i]);
- Scrie(n, a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement