[遇见时光]剑指offer-顺时针打印矩阵java

题目描述:

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

输入:

输入可能包含多个测试样例,对于每个测试案例,

输入的第一行包括两个整数m和n(1<=m,n<=1000):表示矩阵的维数为m行n列。

接下来的m行,每行包括n个整数,表示矩阵的元素,其中每个元素a的取值范围为(1<=a<=10000)。

输出:

对应每个测试案例,输出一行,

按照从外向里以顺时针的顺序依次打印出每一个数字,每个数字后面都有一个空格。

样例输入:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
样例输出:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

Java代码实现
 1 public class circleOutput {
 2     public static void printMatrixIncircle(int[][] nArr, int rows, int columns,
 3             int nStart) {
 4         // nEndX是最右一列列号
 5         int nEndX = columns - 1 - nStart;
 6         // nEndY是最下面一行行号
 7         int nEndY = rows - 1 - nStart;
 8 
 9         // 从左至右打印一行
10         System.out.println();
11         System.out.println("从左至右打印一行");
12         for (int i = nStart; i <= nEndX; i++) {
13         
14             System.out.print(nArr[nStart][i] + "	");
15         }
16 
17         // 从上到下打印一列
18         
19         if (nEndY > nStart) {
20             System.out.println();
21             System.out.println("从上到下打印一列");
22             for (int j = nStart + 1; j <= nEndY; j++) {
23                 
24                 System.out.print(nArr[j][nEndX] + "	");
25             }
26         }
27         // 从右至左打印一行
28         
29         if (nEndX > nStart && nEndY > nStart) {
30             System.out.println();
31             System.out.println("从右至左打印一行");
32             for (int k = nEndX - 1; k >= nStart; k--) {
33                 System.out.print(nArr[nEndY][k] + "	");
34             }
35         }
36         // 从下到上打印一列
37         
38         if (nEndY - 1 > nStart && nEndX > nStart) {
39             System.out.println();
40             System.out.println("从下到上打印一列");
41             for (int p = nEndY - 1; p >= nStart + 1; p--) {
42                 System.out.print(nArr[p][nStart] + "	");
43             }
44         }
45 
46     }
47 
48     public static void print(int[][] nArr, int rows, int columns) {
49         if (nArr == null || rows <= 0 || columns <= 0) {
50             return;
51         }
52         int nStart = 0;
53         while (rows > (nStart * 2) && columns > (nStart * 2)) {
54             printMatrixIncircle(nArr, rows, columns, nStart);
55             nStart++;
56         }
57     }
58 
59     public static void main(String[] args) {
60         // TODO Auto-generated method stub
61         int[][] nMatrix1 = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
62                 { 13, 14, 15, 16 } };
63         print(nMatrix1, 4, 4);    
64         /*
65         int[][] nMatrix2 = {{1,2,3,4}};
66         circleOutput.print(nMatrix2, 1, 4);
67         
68         int[][] nMatrix3 ={{1,2,3,4,5},{6,7,8,9,10}};
69         circleOutput.print(nMatrix3, 2, 5);
70         
71         int[][] nMatrix4 = {{1},{2},{3},{4}};
72         circleOutput.print(nMatrix4, 4, 1);
      */
73 } 74 }

输出结果:

从左至右打印一行
1    2    3    4    
从上到下打印一列
8    12    16    
从右至左打印一行
15    14    13    
从下到上打印一列
9    5    
从左至右打印一行
6    7    
从上到下打印一列
11    
从右至左打印一行
10


原文地址:https://www.cnblogs.com/yujianshiguang/p/5719314.html