题目:顺时针打印矩阵
void printnumber(int x) { cout << x << " " ; } void PrintMatrixInCircle(int (*num)[4], int cols , int rows, int start) { int endx = cols - 1 - start; int endy = rows - 1 - start; //从左到右打印一行 for (int i = start; i <= endx; ++i) { int number = num [start][i]; printnumber(number); } //从上到下打印一列 if (start < endy) { for (int i = start + 1; i <= endy; ++i) { int number = num [i][endx]; printnumber(number); } } //从右到左打印一行 if (start < endx&&start < endy) { for (int i = endx - 1; i >= start; --i) { int number = num [endy][i]; printnumber(number); } } //从下到上打印一行 if (start < endx&&start<endy-1) { for (int i = endy - 1; i >= start + 1; --i) { int number = num [i][start]; printnumber(number); } } } void PrintMatrixClockwisely(int (*num)[4], int cols , int rows) { if (num == NULL || cols <= 0 || rows <= 0) return; int start = 0; while (cols > start * 2 && rows > start * 2) { PrintMatrixInCircle( num, cols , rows, start); ++start; } }