题目:
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
解法:
void print(int a[4][3], int m, int n) {
int mk = m/2 + m%2;
int nk = n/2 + n%2;
int c = mk > nk ? nk : mk;
for (int k = 0; k < c; ++k) {
for (int j = k; j < n-k; ++j) {
cout << a[k][j] << " ";
}
for (int i = k+1; i < m-k; ++i) {
cout << a[i][n-1-k] << " ";
}
for (int j = n-2-k; j >= k; --j) {
cout << a[m-1-k][j] << " ";
}
for (int i = m-2-k; i >= k+1; --i) {
cout << a[i][k] << " ";
}
}
}