Prove safety offer: Clockwise printing matrix (java)

Title: Enter a matrix, in order from outside to inside in a clockwise order successively print out each number. For example: if the input matrix is ​​as follows:

1,2,3,4

5,6,7,8

9,10,11,12

13,14,15,16

Then click Print out the digital 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    Because it is sequentially printed sequentially from the outer to the inner ring, the matrix we can imagine several turns, as shown in FIG. We can use a loop to print matrix, each print a circle matrix.

    Next, the end of cycle analysis condition, assuming that the number of rows of the matrix rows is the number of columns is Columns, printing of the first lap top left corner coordinates are (0,0), the coordinates of the upper left corner is the second ring (1, 1), and so on. We note that the coordinates of the upper left corner of rows and columns labeled standard is always the same. Then you can select the upper left corner of the matrix is (start, start) lap as the goal of our analysis. When n * n matrix, the most is circle (startX, startY), then there is a maximum line length (startY-1) * 2 elements, a maximum of (startX-1) * 2 elements of each column, so rows > startY * 2 and columns> startX * 2.

    Next, we consider how to print a circle of functions, namely how to achieve PrintMatrixInCircle. We can print in a circle divided into four steps: the first step from one line to do the right print, print a line from top to bottom the second step, the third step print a line from right to left, the fourth step from the bottom to print one. Each step we can use a loop or a line is printed out according to the start coordinates. But it is worth noting that last lap there may degenerate into only one line, and only one, or even only a number, so print this circle will no longer need four steps.

    因此我们要仔细分析打印时的每一步的前提条件。第一步总是需要的,因为打印一圈至少需要一步。如果只有一行,那么就不用第二步了。也就是需要第二步的前提条件是终止号大于起止号。需要第三步打印的前提条件是圈内至少有两行两列,也就是说除了要求终止行号大于起始行号之外,还要求终止列号大于起始列号。同理,需要打印第四步的前提条件是至少有三行两列,因此要求终止行号比其实行号至少大2,同时终止列号大于起始列号。

public void printMatrixInCircle(int[][] array){  
        if(array == null)  
            return;  
        int start = 0;  
        while(array[0].length > start*2 && array.length >start*2){  
            printOneCircle(array,start);  
            start++;  
        }  
    }  
    private void printOneCircle(int[][] array,int start){  
        int columns = array[0].length;  
        int rows = array.length;  
        int endX = columns - 1 - start;  
        int endY = rows - 1 - start;  
        //从左到右打印一行  
        for(int i = start;i <= endX ;i++){  
            int number = array[start][i];  
            System.out.print(number+",");  
        }  
        //从上到下打印一列  
        if(start <endY){  
            for(int i = start +1;i<=endY;i++){  
                int number = array[i][endX];  
                System.out.print(number+",");  
            }  
        }  
        //从右到左打印一行  
        if(start < endX && start < endY){  
            for(int i = endX -1;i>=start;i--){  
                int number = array[endY][i];  
                System.out.print(number+",");  
            }  
        }  
        //从下到上打印一列  
        if(start <endY && start <endY -1){  
            for(int i =endY -1;i>=start+1;i--){  
                int number = array[i][start];  
                System.out.print(number+",");  
            }  
        }  
    }  


发布了118 篇原创文章 · 获赞 35 · 访问量 12万+

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52725397