Загрузка...

Laba c++

Thread in C/C++ created by bomjorii Sep 29, 2022. 210 views

  1. bomjorii
    bomjorii Topic starter Sep 29, 2022 https://zelenka.guru/threads/5035223/ фри 100р 31 Aug 25, 2021
    В одномерном массиве все нулевые элементы перенести
    в конец массива, дополнительный массив использовать не разрешается. Посчитать количество перемещений.
     
  2. brediska
    brediska Oct 3, 2022 Banned 2794 May 30, 2021
    bomjorii,
    C
    #include <algorithm>
    #include <array>
    #include <iomanip>
    #include <iostream>
    #include <random>
    using namespace std;
    using array_t = array<int, 20>;
    void show(const array_t& box, const streamsize w) {
    for (auto x : box) cout << setw(w) << x;
    puts("");
    }
    array_t create_random_array(int a, int b) {
    if (a > b) swap(a, b);
    uniform_int_distribution<> uid(a, b);
    mt19937 gen{ random_device()() };
    array_t box{};
    for (auto& x : box) x = uid(gen);
    return box;
    }
    size_t move_zero(array_t& box) {
    auto zero = count(box.begin(), box.end(), 0);
    auto target = box.begin() + box.size() - zero;
    auto tail = count(target, box.end(), 0);
    size_t quantity = zero - tail;
    auto comp = [](int a, int b) { return a && !b; };
    sort(box.begin(), box.end(), comp);
    return quantity;
    }
    int main() {
    auto box = create_random_array(-2, 2);
    show(box, 4);
    auto n = move_zero(box);
    show(box, 4);
    cout << "Permutations: " << n << '\n';
    }
     
Top
Loading...