劍指Offer第20題(順時針打印矩陣)

(本博客旨在個人總結回顧)

題目描述:

       輸入一個矩陣,按照從外向裏以順時針的順序依次打印出每一個數字。例如 :如果輸入如下矩陣:

解題思路:

問題明顯需要需要和判斷臨界條件來輸出結果。

所以需要清晰打印思路:一層一層打印,先打印出第一層,再打印出第二層,依次打印完。

完整代碼: 

#include "stdafx.h"
#include <iostream>
using namespace std;


/*
 * @name   PrintMatrixRect
 * @brief  順時針打印矩陣某一層(從外到裏)
 * @param  [in] int** pMatrix       二維數組(矩陣)
 * @param  [in] int nColumns        行數
 * @param  [in] int nRows           列數
 * @param  [in] int nIndex          (第幾層,從0開始,0爲最外層)    
 * @return void
 */
void PrintMatrixRect(int** pMatrix, int nColumns, int nRows, int nIndex)
{
    if (NULL == pMatrix || nColumns <= 0 || nRows <= 0)
    {
        return;
    }
    //輸出上行
    int rowTop = nIndex;
    int column = nIndex;
    for (; column < nColumns - nIndex; column++)
    {
        cout << pMatrix[rowTop][column] << " ";
    }
    //輸出右列
    int row = nIndex + 1;
    int columnRight = nColumns - 1 - nIndex;
    for (; row < nRows - nIndex; row++)
    {
        cout << pMatrix[row][columnRight] << " ";
    }
    //輸出下行
    int rowBottom = nRows - 1 - nIndex;
    if (rowBottom != rowTop)
    {        
        column = nColumns - nIndex - 2;
        for (; column > nIndex; column--)
        {
            cout << pMatrix[rowBottom][column] << " ";
        }
    }
    //輸出左列
    int columnLeft = nIndex;
    if (columnLeft != columnRight)
    {
        row = nRows - 1 - nIndex;
        for (; row > nIndex; row--)
        {
            cout << pMatrix[row][column] << " ";
        }
    }
}

/*
 * @name   PrintMatrixClockwisely
 * @brief  順時針從外往裏打印矩陣
 * @param  [in] int * * pMatrix 二維數組指針(矩陣)
 * @param  [in] int nColumns    列數
 * @param  [in] int nRows       行數
 * @return void
 */
void PrintMatrixClockwisely(int** pMatrix, int nColumns, int nRows)
{
    if (NULL == pMatrix || nColumns <= 0 || nRows <= 0)
    {
        return;
    }
    int nIndex = 0;
    while (nIndex <= nColumns / 2 && nIndex <= nRows / 2)
    {
        PrintMatrixRect(pMatrix, nColumns, nRows, nIndex);
        nIndex++;
    }
    cout << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //測試例子:多行多列
    int arr1[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };    
    int *array1[4];//指針數組
    for (int i = 0; i < 4; i++)
    {
        array1[i] = arr1[i];
    }
    PrintMatrixClockwisely(array1, 4, 4);

    //測試例子:一行多列
    int arr2[1][4] = {
        { 1, 2, 3, 4 }
    };
    int *array2[1];
    for (int i = 0; i < 1; i++)
    {
        array2[i] = arr2[i];
    }
    PrintMatrixClockwisely(array2, 4, 1);

    //測試例子:多行一列
    int arr3[4][1] = {
        { 1},
        { 5},
        { 9},
        { 13}
    };
    int *array3[4];//指針數組
    for (int i = 0; i < 4; i++)
    {
        array3[i] = arr3[i];
    }
    PrintMatrixClockwisely(array3, 1, 4);

    //測試例子:一行一列
    int arr4[1][1] = {
        { 1 }
    };
    int *array4[1];
    for (int i = 0; i < 1; i++)
    {
        array4[i] = arr4[i];
    }
    PrintMatrixClockwisely(array4, 1, 1);
    PrintMatrixClockwisely(NULL, 1, 1);
    system("pause");
    return 0;
}

運行結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章