我正在尝试打印矩阵的右半球。如果我们在矩阵中绘制主对角线和次对角线,我们会看到我们有4个相等的部分,右边的部分被称为(在我的算法教科书中)正方形矩阵的右半球。
例如,在以下5 x 5
矩阵中,右半球由以下元素组成:-1, -3, -2, 0
。
我试图解决这个问题的方法是先启动并合成一半辅助对角线,然后打印左对角线元素右侧的每个元素。在我到达次要对角线的中间后,我在主对角线的下部重复这个过程。
这样的事情(至少,这是我在脑海中看到的):
以下是一些打印5 x 5
矩阵右半球的工作代码。它有效,但它很难看,它不能正常工作的矩阵和行数都是偶数,例如4 x 4
矩阵。
#include <iostream>
#define N 5
#define M 5
void printHemisphere(int matrix[N][M], int n, int m)
{
int i = 1;
for(int j = n - 1; j > n / 2; i++, j--)
{
for (int k = j + 1; k < m; ++k)
{
std::cout << matrix[i][k] << " ";
}
std::cout << std::endl;
}
for(int j = n / 2; j < n; i++, j++)
{
for (int k = j + 1; k < m; ++k)
{
std::cout << matrix[i][k] << " ";
}
std::cout << std::endl;
}
}
int main(int argc, char const *argv[])
{
int matrix5[N][M] =
{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
printHemisphere(matrix5, N, M);
return 0;
}
你会如何解决这个问题?
答案 0 :(得分:2)
我认为这适用于方形矩阵:
void printHemisphere(int matrix[N][M], int n, int m)
{
int mid = n / 2;
for(int i = 1; i < mid; i++)
{
for (int j = n - i; j < m; ++j)
{
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
for(int i = mid; i < n - 1; i++)
{
for (int j = i + 1; j < m; ++j)
{
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
}
外部循环跳过第一行和最后一行,因为没有输出可以来自它们。
答案 1 :(得分:1)
这是一种只需双循环的方法:
void printHemisphere(int matrix[N][M], int n, int m)
{
for(int i = 1; i < n - 1 ; i++)
{
for (j = max(i, n - i) ; j < m ; j++)
{
std::cout << matrix[i][k] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
答案 2 :(得分:1)
在伪代码中......:
for row in 1 -> height - 2 // Indices between 0 -> height - 1
distance = min(row, height - 1 - row)
for cell in (width - distance) -> width - 1
print matrix[row][cell]
答案 3 :(得分:1)
这是我对它的看法。我来晚了一点,因为你已经接受了解决方案;不过,我想告诉你一些事情。例如,如果您将这些矩阵视为单维数组,则只需使用一个函数就可以测试所有大小的矩阵,因为大小不必是&#34;内置于&#34;函数定义中的数据类型。我已经对它做了很多评论,希望尽可能清楚。
#include <iostream>
#include <algorithm>
// I store matrices as a monodimensional array, so that it is easier to deal
// with matrices of different sizes in the same program, because you can do
// everything with one function. Moreover, I use only one argument here, to make
// it clear that we are dealing with square matrices.
void printHemisphere(int* matrix, int last_row)
{
int last_column = last_row; // It's a square matrix, so they are the same.
// Still, distinguishing between last_column
// and last_row can make the algorithm clearer,
// so I have kept both
// In general I prefer to use "row" and "column" as variable names, instead
// of "i" and "j"
// Our rows go from 0 to last_row-1, but since the first and last one can
// certainly not be used, we can skip them: we start at row = 1, and we stop
// at last_row - 2 (that is, the last one for which row < last_row - 1)
for(int row = 1; row < last_row - 1 ; row++)
{
// We want to start from the cell to the right of the rightmost diagonal.
// The main diagonal has column = row;
// The secondary diagonal has column = last_row - 1 - row
// The rightmost one is the maximum of these 2.
// Then, we want to take the cell to the right of the diagonal, so we
// have to add 1 more.
// All in all we have:
for (int column = std::max(row, last_row - 1 - row) + 1;
column < last_column;
column++)
{
// since this is a 1-D array we have to access it this way
std::cout << matrix[row*last_row+column] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
int main(int argc, char const *argv[])
{
// Since I am working with monodimensional arrays, I don't store them as
// int matrix3[3][3], but rather as int matrix3[9], which I have expressed as
// int matrix3[3*3] for clarity.
//For each one I have indicated the expected output.
// Expected output:
// 6
int matrix3[3*3] =
{
1, 2, 3,
4, 5, 6,
7, 8, 9
};
// Expected output:
// 8
// 12
int matrix4[4*4] =
{
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
// Expected output:
// 10
// 14 15
// 20
int matrix5[5*5] = {
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25
};
// Expected output:
// 12
// 17 18
// 23 24
// 30
int matrix6[6*6] = {
1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36
};
// Expected output:
// 14
// 20 21
// 26 27 28
// 34 35
// 42
int matrix7[7*7] = {
1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49
};
printHemisphere(matrix3, 3);
printHemisphere(matrix4, 4);
printHemisphere(matrix5, 5);
printHemisphere(matrix6, 6);
printHemisphere(matrix7, 7);
return 0;
}
我已经在ideone上验证了它并且它有效。我唯一想补充的是:为了确保代码有效,请确保使用奇数和偶数大小的矩阵进行测试。