順時針列印矩陣

weixin_34026276發表於2017-11-27

   輸入一個矩陣,按照從外向裡以順時針的順序依次列印出每一個數字。


    解題思路:我們把列印一圈分為四步:第一步從左到右列印一行,第二步從上到下列印一列,第三步從右到左列印一行,第四步從下到上列印一列。值得注意的是,最後一圈有可能退化成只有一行、只有一列,甚至只有一個數字。

    因此要仔細分析列印時每一步的前提條件。第一步總是需要的,因為列印一圈至少有一步。如果只有一行,就不用第二步了。也就是需要第二步的前提條件是終止行號大於起始行號。需要第三部列印的前提條件是圈內至少有兩行兩列,也就是說除了要求終止行號大於起始行號之外,還要求終止列號大於起始列號。需要列印第四步的前提條件是至少有三行兩列,因此要求終止行號比起始行號至少大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 in range(start, endX+1):
        print(numbers[start][i], end=', ')
    # 從上到下列印一列
    if start < endY:
        for in range(start+1, endY+1):
            print(numbers[i][endX], end=', ')
    # 從右到左列印一行
    if start < endX and start < endY:
        for in range(endX - 1, start-1-1):
            print(numbers[endY][i], end=', ')
    # 從下到上列印一行
    if start < endX and start < endY - 1:
        for in range(endY-1, start, -1):
            print(numbers[i][start], end=', ')



本文轉自 許大樹 51CTO部落格,原文連結:http://blog.51cto.com/abelxu/1973293,如需轉載請自行聯絡原作者

相關文章