关于矩阵的题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于矩阵的题相关的知识,希望对你有一定的参考价值。

题目:顺时针打印矩阵

void printnumber(int x)
{
                cout << x << " " ;
}
void PrintMatrixInCircle(int (*num)[4], int cols , int rows, int start)
{
                 int endx = cols - 1 - start;
                 int endy = rows - 1 - start;
                 //从左到右打印一行
                 for (int i = start; i <= endx; ++i)
                {
                                 int number = num [start][i];
                                printnumber(number);
                }
                 //从上到下打印一列
                 if (start < endy)
                {
                                 for (int i = start + 1; i <= endy; ++i)
                                {
                                                 int number = num [i][endx];
                                                printnumber(number);
                                }
                }
                 //从右到左打印一行
                 if (start < endx&&start < endy)
                {
                                 for (int i = endx - 1; i >= start; --i)
                                {
                                                 int number = num [endy][i];
                                                printnumber(number);
                                }
                }
                 //从下到上打印一行
                 if (start < endx&&start<endy-1)
                {
                                 for (int i = endy - 1; i >= start + 1; --i)
                                {
                                                 int number = num [i][start];
                                                printnumber(number);
                                }
                }
}
void PrintMatrixClockwisely(int (*num)[4], int cols , int rows)
{
                 if (num == NULL || cols <= 0 || rows <= 0)
                                 return;
                 int start = 0;

                 while (cols > start * 2 && rows > start * 2)
                {
                                PrintMatrixInCircle( num, cols , rows, start);
                                ++start;
                }
}


本文出自 “无以伦比的暖阳” 博客,请务必保留此出处http://10797127.blog.51cto.com/10787127/1775973

以上是关于关于矩阵的题的主要内容,如果未能解决你的问题,请参考以下文章

剑指Offer12 矩阵中的路径(有趣的题)

Python代码阅读(第41篇):矩阵转置

[JavaScript 刷题] Code Signal - 矩阵元素之和(matrixElementsSum)

关于代码片段的时间复杂度

NLP一文了解词性标注CRF模型

如何将浮点矩阵作为 2D 纹理传递给片段着色器?