Advertisement
a53

hard_prime

a53
May 9th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("hard_prime.in");
  4. ofstream fout("hard_prime.out");
  5. bool isPrime(int nr)
  6. {
  7. if (nr <= 1)
  8. return false;
  9. if (nr <= 3)
  10. return true;
  11. if (nr % 2 == 0 || nr % 3 == 0)
  12. return false;
  13. for (int i = 5; i * i <= nr; i = i + 6)
  14. if (nr % i == 0 || nr % (i + 2) == 0)
  15. return false;
  16. return true;
  17. }
  18.  
  19. int main()
  20. {
  21. int n, nr;
  22. fin>>n;
  23. vector <int>a;
  24. vector<int>::iterator ip;
  25. for(int i=0; i<=n; i++)
  26. {
  27. fin>>nr;
  28. if(nr==2)
  29. a.push_back(nr);
  30. else if(nr%2)
  31. if(isPrime(nr))
  32. a.push_back(nr);
  33. }
  34. sort(a.begin(), a.end());
  35. ip = unique(a.begin(), a.begin() + a.size());
  36. a.resize(distance(a.begin(), ip));
  37. for(ip = a.begin(); ip != a.end(); ++ip)
  38. fout << *ip << " ";
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement