码迷,mamicode.com
首页 > 其他好文 > 详细

关于矩阵的题

时间:2016-05-23 01:12:15      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:剑指offer

题目:顺时针打印矩阵

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

关于矩阵的题

标签:剑指offer

原文地址:http://10797127.blog.51cto.com/10787127/1775973

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!