输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
解题思路:我们把打印一圈分为四步:第一步从左到右打印一行,第二步从上到下打印一列,第三步从右到左打印一行,第四步从下到上打印一列。值得注意的是,最后一圈有可能退化成只有一行、只有一列,甚至只有一个数字。
因此要仔细分析打印时每一步的前提条件。第一步总是需要的,因为打印一圈至少有一步。如果只有一行,就不用第二步了。也就是需要第二步的前提条件是终止行号大于起始行号。需要第三部打印的前提条件是圈内至少有两行两列,也就是说除了要求终止行号大于起始行号之外,还要求终止列号大于起始列号。需要打印第四步的前提条件是至少有三行两列,因此要求终止行号比起始行号至少大2,终止列号大于起始列号。
C#实现:
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
|
#region 顺时针打印矩阵
/// 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
///
public
static
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++;
}
}
private
static
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++)
Console.Write(numbers[start, i] +
","
);
// 从上到下打印一列
if
(start < endY)
for
(
int
i = start + 1; i <= endY; i++)
Console.Write(numbers[i, endX] +
","
);
// 从右到左打印一行
if
(start < endX && start < endY)
for
(
int
i = endX - 1; i >= start; i--)
Console.Write(numbers[endY, i] +
","
);
// 从下到上打印一行
if
(start < endX && start < endY - 1)
for
(
int
i = endY - 1; i >= start + 1; i--)
Console.Write(numbers[i, start] +
","
);
}
#endregion
|
Java实现:
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
|
/** 顺时针打印矩阵
* 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
*/
public
static
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++;
}
}
private
static
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++)
System.out.print(numbers[start][i] +
","
);
// 从上到下打印一列
if
(start < endY)
for
(
int
i = start +
1
; i <= endY; i++)
System.out.print(numbers[i][endX] +
","
);
// 从右到左打印一行
if
(start < endX && start < endY)
for
(
int
i = endX -
1
; i >= start; i--)
System.out.print(numbers[endY][i] +
","
);
// 从下到上打印一行
if
(start < endX && start < endY -
1
)
for
(
int
i = endY -
1
; i >= start +
1
; i--)
System.out.print(numbers[i][start] +
","
);
}
|
Python实现:
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
|
def
printMatrixClockWisely(numbers, columns, rows):
"""
顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
:param numbers:
:param columns:
:param rows:
:return:
"""
if
numbers
=
=
None
or
columns <
=
0
or
rows <
=
0
:
return
start
=
0
while
columns > start
*
2
and
rows > start
*
2
:
printMatrixInCircle(numbers, columns, rows, start)
start
+
=
1
def
printMatrixInCircle(numbers, columns, rows, start):
endX
=
columns
-
1
-
start
endY
=
rows
-
1
-
start
# 从左到右打印一行
for
i
in
range
(start, endX
+
1
):
print
(numbers[start][i], end
=
', '
)
# 从上到下打印一列
if
start < endY:
for
i
in
range
(start
+
1
, endY
+
1
):
print
(numbers[i][endX], end
=
', '
)
# 从右到左打印一行
if
start < endX
and
start < endY:
for
i
in
range
(endX
-
1
, start
-
1
,
-
1
):
print
(numbers[endY][i], end
=
', '
)
# 从下到上打印一行
if
start < endX
and
start < endY
-
1
:
for
i
in
range
(endY
-
1
, start,
-
1
):
print
(numbers[i][start], end
=
', '
)
|
本文转自 许大树 51CTO博客,原文链接:http://blog.51cto.com/abelxu/1973293,如需转载请自行联系原作者