Guest User

Untitled

a guest
Nov 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <random>
  4. using namespace std;
  5. using box_t = pair<int*, const size_t>;
  6. class Array {
  7. public:
  8.     static void fill(box_t& box, const int a, const int b, const int m) {
  9.         auto[sequence, length] = box;
  10.         for (auto i = 0U; i < length; ++i) sequence[i] = generate(a, b) * m;
  11.     }
  12.     static void show(const box_t& box, const streamsize width = 5U) {
  13.         auto[sequence, length] = box;
  14.         for (auto i = 0U; i < length; ++i) cout << setw(width) << sequence[i];
  15.         cout.put('\n');
  16.     }
  17.     static pair<int, int> min_even(const box_t& box) {
  18.         auto[sequence, length] = box;
  19.         auto min = numeric_limits<int>::max();
  20.         auto index = -1;
  21.         for (auto i = 0U; i < length; ++i)
  22.             if (0 == sequence[i] % 2 && sequence[i] < min) {
  23.                 min = sequence[i];
  24.                 index = i;
  25.             }
  26.         return { index, min };
  27.     }
  28.     static pair<int, int> max_odd(const box_t& box) {
  29.         auto[sequence, length] = box;
  30.         auto max = numeric_limits<int>::min();
  31.         auto index = -1;
  32.         for (auto i = 0U; i < length; ++i)
  33.             if (sequence[i] & 1 && sequence[i] > max) {
  34.                 max = sequence[i];
  35.                 index = i;
  36.             }
  37.         return { index, max };
  38.     }
  39.     static void split_and_reverse(box_t& box, const size_t parts) {
  40.         auto[sequence, length] = box;
  41.         const auto part = length / parts;
  42.         for (auto i = 0U; i < length; i += part) {
  43.             const auto beg = sequence + i;
  44.             auto last = beg + part - 1;
  45.             if (i + part > length) {
  46.                 const auto tail = i + part - length;
  47.                 last -= tail;
  48.             }
  49.             reverse(beg, last);
  50.         }
  51.     }
  52. private:
  53.     static int generate(int a, int b) {
  54.         if (a > b) swap(a, b);
  55.         const uniform_int_distribution<> uid(a, b);
  56.         random_device rd;
  57.         mt19937 gen(rd());
  58.         return uid(gen);
  59.     }
  60.     static void reverse(int* beg, int* last) {
  61.         while (beg < last) swap(*beg++, *last--);
  62.     }
  63. };
  64. int main() {
  65.     const auto n = 40U;
  66.     const auto l = -14, r = 14, m = 10;
  67.     int a[n];
  68.     box_t box{ a, n };
  69.     Array::fill(box, l, r, m);
  70.     Array::show(box);
  71.     auto[index_min, min_even] = Array::min_even(box);
  72.     if (index_min != -1) cout << "Min value: " << min_even << "; index: " << index_min << '\n';
  73.     // Посе умножении любого числа на чётное оно также становится чётным! И тем не менее...
  74.     auto[index_max, max_odd] = Array::max_odd(box);
  75.     if (index_max != -1) cout << "Max value: " << max_odd << "; index: " << index_max << '\n';
  76.     Array::split_and_reverse(box, 10U);
  77.     Array::show(box);
  78.     system("pause");
  79. }
Advertisement
Add Comment
Please, Sign In to add comment