# 【面试题二十】顺时针打印矩阵

PrintMatrix.cpp:

#include <iostream>
#include <cstdio>

using namespace std;

void PrintMatrixInCircle(int ** numbers, int columns, int rows, int start);
void PrintNumber(int number);

void PrintMatrixClockwisely(int** numbers, int columns, int rows)
{
if(numbers == NULL || columns <= 0 || rows <= 0)
{
return ;
}

int start = 0;
while(columns > start * 2 && rows > start * 2)
{
PrintMatrixInCircle(numbers, columns, rows, start);
++start;
}
}

void PrintMatrixInCircle(int **numbers, int columns, int rows, int start)
{
int endX = columns - 1 - start;
int endY = rows - 1 - start;

/*从左到右打印一行*/
for(int i = start; i <= endX; ++i)
{
int number = numbers[start][i];
PrintNumber(number);
}
/*从上到下打印一列*/
if(start < endY)
{
for(int i = start + 1; i <= endY; ++i)
{
int number = numbers[i][endX];
PrintNumber(number);
}
}
/*从右到左打印一行*/
if(start < endX && start < endY)
{
for(int i = endX - 1; i >= start; --i)
{
int number = numbers[endY][i];
PrintNumber(number);
}
}
/*从下到上打印一行*/
if(start < endX && start < endY - 1)
{
for(int i = endY - 1; i >= start + 1; --i)
{
int number = numbers[i][start];
PrintNumber(number);
}
}
}

void PrintNumber(int number)
{
cout<<number<<"\t";
}

// ====================测试代码====================
void Test(int columns, int rows)
{
printf("Test Begin: %d columns, %d rows.\n", columns, rows);

if(columns < 1 || rows < 1)
return;

int** numbers = new int*[rows];
for(int i = 0; i < rows; ++i)
{
numbers[i] = new int[columns];
for(int j = 0; j < columns; ++j)
{
numbers[i][j] = i * columns + j + 1;
}
}

PrintMatrixClockwisely(numbers, columns, rows);
printf("\n");

for(int i = 0; i < rows; ++i)
delete[] (int*)numbers[i];

delete[] numbers;
}

int main()
{
/*
1
*/
Test(1, 1);

/*
1    2
3    4
*/
Test(2, 2);

/*
1    2    3    4
5    6    7    8
9    10   11   12
13   14   15   16
*/
Test(4, 4);

/*
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
*/
Test(5, 5);

/*
1
2
3
4
5
*/
Test(1, 5);

/*
1    2
3    4
5    6
7    8
9    10
*/
Test(2, 5);

/*
1    2    3
4    5    6
7    8    9
10   11   12
13   14   15
*/
Test(3, 5);

/*
1    2    3    4
5    6    7    8
9    10   11   12
13   14   15   16
17   18   19   20
*/
Test(4, 5);

/*
1    2    3    4    5
*/
Test(5, 1);

/*
1    2    3    4    5
6    7    8    9    10
*/
Test(5, 2);

/*
1    2    3    4    5
6    7    8    9    10
11   12   13   14    15
*/
Test(5, 3);

/*
1    2    3    4    5
6    7    8    9    10
11   12   13   14   15
16   17   18   19   20
*/
Test(5, 4);

return 0;
}



Makefile:

.PHONY:clean
CPP=g++
CFLAGS=-Wall -g
BIN=test
OBJS=PrintMatrix.o
LIBS=
$(BIN):$(OBJS)
$(CPP)$(CFLAGS) $^ -o$@ $(LIBS) %.o:%.cpp$(CPP) $(CFLAGS) -c$< -o $@ clean: rm -f *.o$(BIN)


Test Begin: 1 columns, 1 rows.
1
Test Begin: 2 columns, 2 rows.
1       2       4       3
Test Begin: 4 columns, 4 rows.
1       2       3       4       8       12      16      15      14      13      9       5       6       7       11      10
Test Begin: 5 columns, 5 rows.
1       2       3       4       5       10      15      20      25      24      23      22      21      16      11      6       7       8       9   14       19      18      17      12      13
Test Begin: 1 columns, 5 rows.
1       2       3       4       5
Test Begin: 2 columns, 5 rows.
1       2       4       6       8       10      9       7       5       3
Test Begin: 3 columns, 5 rows.
1       2       3       6       9       12      15      14      13      10      7       4       5       8       11
Test Begin: 4 columns, 5 rows.
1       2       3       4       8       12      16      20      19      18      17      13      9       5       6       7       11      15      14  10
Test Begin: 5 columns, 1 rows.
1       2       3       4       5
Test Begin: 5 columns, 2 rows.
1       2       3       4       5       10      9       8       7       6
Test Begin: 5 columns, 3 rows.
1       2       3       4       5       10      15      14      13      12      11      6       7       8       9
Test Begin: 5 columns, 4 rows.
1       2       3       4       5       10      15      20      19      18      17      16      11      6       7       8       9       14      13  12

#### 剑指Offer面试题20：顺时针打印矩阵 Java实现

2016-09-10 16:52:01

#### 剑指Offer面试题20（Java版）：顺时针打印矩阵

2015-08-02 20:41:00

#### 剑指Offer:面试题20——顺时针打印矩阵(java实现)

2016-07-07 10:06:00

#### 笔试题&面试题：顺时针输出一个矩阵

2014-06-27 09:45:39

#### 基于python超简洁的顺时针打印矩阵

2017-03-09 16:39:16

#### 剑指offer之面试题20：顺时针打印矩阵

2016-04-25 16:14:25

#### 剑指Offer20题顺时针打印矩阵（欢迎批评指正）Java版

2016-10-04 02:30:47

#### 剑指offer--面试题20:顺时针打印矩阵--Java实现

2015-06-19 19:44:00

#### LeetCode | Spiral Matrix（顺时针打印矩阵）

2014-08-11 11:11:34

#### 经典算法——顺时针打印矩阵

2016-03-09 10:21:48