Дан двумерный массив(5 на 5) и надо все нули этого массива переставить в конец. решение на С++
По дате
По рейтингу
По аналогии на плюсах напишешь
123456789101112131415161718192021
def print_arr(array : list):
for i in range(4):
print(array[i])
arr = [[0, 3, 1, 0, 4, 7, 0],
[9, 8, 3, 0, 0, 6, -2],
[6, 0, 0, 2, 4, 0, 1],
[0, 0, 4, 7, -1, -7, -3]
]
print_arr(arr)
for i in range(4):
k = 0
for j in range(7):
if arr[i][j] != 0:
arr[i][k], arr[i][j] = arr[i][j], arr[i][k]
k = k + 1
print()
print_arr(arr)
123456789101112131415161718192021222324252627282930
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
using namespace std;
int main() {
constexpr size_t n = 5;
int matrix[n][n]{};
uniform_int_distribution<> uid(0, 9);
mt19937 gen{ random_device()() };
for (auto& row : matrix) {
for (auto& value : row) {
value = uid(gen);
cout << setw(4) << value;
}
cout.put('\n');
}
cout.put('\n');
constexpr auto x = 0;
auto lambda = [x](int value) { return value != x; };
stable_partition(&matrix[0][0], &matrix[n - 1][n], lambda);
for (const auto& row : matrix) {
for (auto& value : row) {
cout << setw(4) << value;
}
cout.put('\n');
}
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const int N = 5;
int a[N][N];
cout << “Enter the elements of the array: \n”;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> a[i][j];
}
}
1234567891011
// Swap zero elements with the last element in each row
for (int i = 0; i < N; i++) {
for (int j = 0, k = N - 1; j < k; j++, k--) {
if (a[i][j] == 0 && a[i][k] != 0) {
int temp = a[i][j];
a[i][j] = a[i][k];
a[i][k] = temp;
}
}
}
Больше по теме