В одномерном массиве все нулевые элементы перенести в конец массива, дополнительный массив использовать не разрешается. Посчитать количество перемещений.
bomjorii, #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'; } 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'; }