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

368人阅读 评论(0)

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

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：48995次
• 积分：918
• 等级：
• 排名：千里之外
• 原创：44篇
• 转载：1篇
• 译文：0篇
• 评论：2条
博客专栏
 剑指offer 文章：26篇 阅读：15075
 剑指offer 文章：0篇 阅读：0
文章分类
阅读排行
最新评论