C/C++

Транспонировать матрицу относительно

побочной диагонали с помощью цикла do while, не использовать дополнительных матриц и массивов.
Так?

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int matrix[][5] = {
{ 1, 3, 7, 13, 0 },
{ 5, 9, 15, 0, 20 },
{ 11, 17, 0, 18, 12 },
{ 19, 0, 16, 10, 6 },
{ 0, 14, 8, 4, 2 },
};
for (const auto& row : matrix) {
for (const auto value : row) cout << setw(4) << value;
puts("\n");
}
puts("\n");
auto n = size(matrix);
for (auto i = 0U; i < n - 1; ++i)
for (auto j = 0U; j < n - 1 - i; ++j) {
matrix[i][j] ^= matrix[n - 1 - j][n - 1 - i];
matrix[n - 1 - j][n - 1 - i] ^= matrix[i][j];
matrix[i][j] ^= matrix[n - 1 - j][n - 1 - i];
}
for (const auto& row : matrix) {
for (const auto value : row) cout << setw(4) << value;
puts("\n");
}
system("pause > nul");
}

Или так?

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int matrix[][5] = {
{ 1, 3, 7, 13, 0 },
{ 5, 9, 15, 0, 20 },
{ 11, 17, 0, 18, 12 },
{ 19, 0, 16, 10, 6 },
{ 0, 14, 8, 4, 2 },
};
for (const auto& row : matrix) {
for (const auto value : row) cout << setw(4) << value;
puts("\n");
}
puts("\n");
auto n = size(matrix) - 1;
for (auto i = 0U, k = n; i < n; ++i, --k) {
for (auto j = 0U, m = n; j < n - i; ++j, --m) {
swap(matrix[i][j], matrix[k][m]);
}
}
for (const auto& row : matrix) {
for (const auto value : row) cout << setw(4) << value;
puts("\n");
}
system("pause > nul");
}
Дмитрий Маркин
Дмитрий Маркин
85 216
Лучший ответ