題目:
輸入一個矩陣玩徊,按照從外向里以順時針的順序依次打印出每一個數(shù)字。
解法:
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] << " ";
}
}
}