Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <random>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7. using box_t = pair<int*, const size_t>;
  8. class Array {
  9. public:
  10.     static void fill(box_t& box, const int a, const int b) {
  11.         auto[sequence, length] = box;
  12.         for (auto i = 0U; i < length; ++i) sequence[i] = generate(a, b);
  13.     }
  14.     static void show(const box_t& box, const streamsize width = 4U) {
  15.         auto[sequence, length] = box;
  16.         for (auto i = 0U; i < length; ++i) cout << setw(width) << sequence[i];
  17.         cout.put('\n');
  18.     }
  19.     static pair<int, int> first_even(const box_t& box) {
  20.         auto[sequence, length] = box;
  21.         for (auto i = 0; i < int(length); ++i) if (0 == sequence[i] % 2) return { i, sequence[i] };
  22.         return { -1, 0 };
  23.     }
  24.     static pair<int, int> last_pos(const box_t& box) {
  25.         auto[sequence, length] = box;
  26.         int pos = length - 1;
  27.         do if (sequence[pos] > 0) break; while (--pos >= 0);
  28.         if (pos < 0) return { -1, 0 };
  29.         return { pos, sequence[pos] };
  30.     }
  31.     static void sort_increase(box_t& box, size_t start, size_t last) {
  32.         auto[sequence, length] = box;
  33.         if (start > last) swap(start, last);
  34.         if (start == last || last >= length) return;
  35.         sort(sequence + start, sequence + last);
  36.     }
  37.     static void show_prime(box_t& box, const streamsize width = 3U) {
  38.         auto[sequence, length] = box;
  39.         vector<size_t> indexes;
  40.         for (auto i = 0; i < int(length); ++i) if (is_prime(abs(sequence[i]))) indexes.push_back(i);
  41.         cout << indexes.size() << ';';
  42.         for (const auto index : indexes) cout << ' ' << index;
  43.         cout.put('\n');
  44.     }
  45. private:
  46.     static int generate(int a, int b) {
  47.         if (a > b) swap(a, b);
  48.         const uniform_int_distribution<> uid(a, b);
  49.         random_device rd;
  50.         mt19937 gen(rd());
  51.         return uid(gen);
  52.     }
  53.     static bool is_prime(const int num) {
  54.         bool prime;
  55.         if (num == 2 || num == 3 || num == 5) prime = true;
  56.         else if (~num & 1 || num < 2 || 0 == num % 3 || 0 == num % 5) prime = false;
  57.         else {
  58.             int n;
  59.             for (n = 3; n * n <= num && num % n; n += 2) { ; }
  60.             prime = n * n > num;
  61.         }
  62.         return prime;
  63.     }
  64. };
  65. int main() {
  66.     const auto n = 27U;
  67.     const auto l = -48, r = 59;
  68.     int a[n];
  69.     box_t box_a{ a, n };
  70.     Array::fill(box_a, l, r);
  71.     cout << "Array a:";
  72.     Array::show(box_a);
  73.     int b[n];
  74.     box_t box_b{ b, n };
  75.     Array::fill(box_b, l, r);
  76.     cout << "Array b:";
  77.     Array::show(box_b);
  78.     auto[index_a_even, value_a_even] = Array::first_even(box_a);
  79.     if (index_a_even != -1) cout
  80.         << "\nThe value of the first even element for a: " << value_a_even
  81.         << "\n The index of the first even element for a: " << index_a_even;
  82.     auto[index_a_pos, value_a_pos] = Array::last_pos(box_a);
  83.     if (index_a_pos != -1) cout
  84.         << "\nThe value of the last positive element for a: " << value_a_pos
  85.         << "\nThe index of the last positive element for a: " << index_a_pos
  86.         << '\n';
  87.     auto[index_b_even, value_b_even] = Array::first_even(box_b);
  88.     if (index_b_even != -1) cout
  89.         << "\nThe value of the first even element for b: " << value_b_even
  90.         << "\nThe index of the first even element for b: " << index_b_even;
  91.     auto[index_b_pos, value_b_pos] = Array::last_pos(box_b);
  92.     if (index_b_pos != -1) cout
  93.         << "\nThe value of the last positive element for b: " << value_b_pos
  94.         << "\nThe index of the last positive element for b: " << index_b_pos
  95.         << '\n';
  96.     Array::sort_increase(box_a, index_a_even + 1, index_a_pos);
  97.     cout << "\nArray a: ";
  98.     Array::show(box_a);
  99.     Array::sort_increase(box_b, index_b_even + 1, index_b_pos);
  100.     cout << "Array b: ";
  101.     Array::show(box_b);
  102.     cout << "The number of prime numbers in a: ";
  103.     Array::show_prime(box_a);
  104.     cout << "The number of prime numbers in b: ";
  105.     Array::show_prime(box_b);
  106.     system("pause");
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement