# 转圈打印矩阵

### 题目描述

##### 示例1
###### 输入
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

###### 输出
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10


##### 代码：
#include <cstdio>

using namespace std;

const int N = 200;

int n, m;
int a[N][N];

int main(void) {
scanf("%d%d", &n, &m);
for ( int i = 0; i < n; ++i ) {
for ( int j = 0; j < m; ++j )
scanf ( "%d", a[i] + j );
}
int tot = n * m;
int now = 0;
int tr = 0, tc = 0;
int dr = n - 1, dc = m - 1;
while ( now < tot ) {
for ( int i = tc; i <= dc; ++i ) {
if ( now++ ) putchar( ' ' );
printf("%d", a[tr][i]);
}
for ( int i = tr + 1; i <= dr; ++i ) {
if ( now++ ) putchar( ' ' );
printf("%d", a[i][dc]);
}
if ( tr < dr ) {
for ( int i = dc - 1; i >= tc; --i ) {
if ( now++ ) putchar( ' ' );
printf("%d", a[dr][i]);
}
}
if ( tc < dc ) {
for ( int i = dr - 1; i > tr; --i ) {
if ( now++ ) putchar( ' ' );
printf("%d", a[i][tc]);
}
}
++tc, ++tr;
--dc, --dr;
}
return 0 * puts("");
}

09-19
05-30 620

05-06 394
12-04 353
06-06 232
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客