Advertisement
codegod313

Order_v

Mar 24th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. vector<int> sieve_of_eratosthenes(int n) {
  8.     vector<bool> prime(n + 1, true);
  9.     prime[0] = prime[1] = false;
  10.     for (int i = 2; i <= n; ++i)
  11.         if (prime[i])
  12.             if (i * 1ll * i <= n)
  13.                 for (int j = i * i; j <= n; j += i)
  14.                     prime[j] = false;
  15.     vector<int> a;
  16.     for (int i = n; i >= 2; i--) {
  17.         if (a.size() == 10)
  18.             break;
  19.         if (prime[i])
  20.             a.push_back(i);
  21.     }
  22.     reverse(a.begin(), a.end());
  23.     return a;
  24. }
  25.  
  26. void test() {
  27.     vector<int> a = sieve_of_eratosthenes(100);
  28.     int chek[] = { 53,59,61,67,71,73,79,83,89,97 };
  29.     bool id = false;
  30.     for (int i = 0; i < 10; i++) {
  31.         if (chek[i] != a[i])
  32.             id = true;     
  33.     }
  34.     if (!id) {
  35.         cout << "Test 1:OK" << endl;
  36.     }
  37.     else
  38.     {
  39.         cout << "Test 1:Error" << endl;
  40.         id = false;
  41.     }
  42.     a = sieve_of_eratosthenes(200);
  43.     int chek2[] = { 157,163,167,173,179,181,191,193,197,199 };
  44.     for (int i = 0; i < 10; i++) {
  45.         if (chek2[i] != a[i])
  46.             id = true;
  47.     }
  48.     if (!id) {
  49.         cout << "Test 2:OK" << endl;
  50.     }
  51.     else
  52.     {
  53.         cout << "Test 2:Error" << endl;
  54.         id = false;
  55.     }
  56.    
  57. }
  58.  
  59. int main()
  60. {
  61.     int n;
  62.     cout << "Enter the number" << endl;
  63.     while (!scanf_s("%d", &n) || n < 0) {
  64.         rewind(stdin);
  65.         cout << "Enter the correct sentence" << endl;
  66.     }
  67.     vector<int> a = sieve_of_eratosthenes(n);
  68.     for (int i = 0; i < a.size(); i++) {
  69.         cout << a[i] << " ";
  70.     }
  71.     cout << endl;
  72.     test();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement