forked from yunshouhu/InterviewQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
面试题20之顺时针打印矩阵_PrintMatrix.cpp
196 lines (163 loc) · 3.32 KB
/
面试题20之顺时针打印矩阵_PrintMatrix.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// PrintMatrix.cpp : Defines the entry point for the console application.
//
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
#include "stdafx.h"
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)
{
printf("%d\t", number);
}
// ====================测试代码====================
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 _tmain(int argc, _TCHAR* argv[])
{
/*
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;
}