/
// 2. 题目20 顺时针打印矩阵
// 输入一个矩阵,按照从外向里以顺时针依次打印出每个数字!!!
void PrintMatirxInCircle(int(*ppArray)[4], int iRows, int iColumns, int iStart) { int iEndX = iColumns - 1 - iStart; int iEndY = iRows - 1 - iStart; // 1.从左到右打印一行 for (int i = iStart; i <= iEndX; i++) { printf("%2d ", ppArray[iStart][i]); } // 2.从上到下打印一列 if (iStart < iEndY) { for (int i = iStart + 1; i < iEndY; i++) { printf("%2d ", ppArray[i][iEndX]); } } // 3.从右到左打印一行 if (iStart < iEndX && iStart < iEndY) { for (int i = iEndX - 1; i >= iStart; i--) { printf("%2d ", ppArray[iEndY][i]); } } // 4.从下到上打印一行 if (iStart < iEndX && iStart < iEndY - 1) { for (int i = iEndY - 1; i >= iStart + 1; i--) { printf("%2d ", ppArray[i][iStart]); } } } void PrintMatirxClockwisely(int (*ppArray)[4], int iRows, int iColumns) { if (NULL == ppArray || iRows <= 0 || iColumns <= 0) { return; } int iStart = 0; while (iColumns > iStart * 2 && iRows > iStart * 2) { // 打印一圈数据 PrintMatirxInCircle(ppArray, iRows, iColumns, iStart); iStart++; } } void PrintMatirxTestFunc() { cout << "\n\n --------------- PrintMatirxTestFunc Start -------------->" << endl; int aiArray[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; PrintMatirxClockwisely(aiArray, 4, 4); cout << "\n\n --------------- PrintMatirxTestFunc End -------------->" << endl; }