zstu.4022.旋转数阵(模拟)

时间:2023-03-09 03:35:49
zstu.4022.旋转数阵(模拟)

旋转数阵

Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1477  Solved: 102

Description

把1到n2的正整数从左上角开始由外层至中心按照顺时针方向螺旋排列

Input

输入整数n (1 <= n <= 10)

Output

按示例输出矩阵

Sample Input

3
4

Sample Output

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

HINT

 #include<stdio.h>
#include<string.h>
const int M = ;
int a[M][M] ;
int main ()
{
int n , x , y , tot = ;
while (~ scanf ("%d" , &n) ) {
memset (a , , sizeof(a)) ;
tot = a[x = ][y = ] = ;
while (tot < n * n) {
while (y + < n && !a[x][y + ]) a[x][++y] = ++tot ;
while (x + < n && !a[x + ][y]) a[++x][y] = ++tot ;
while (y - >= && !a[x][y - ]) a[x][--y] = ++tot ;
while (x - >= && !a[x - ][y]) a[--x][y] = ++tot ;
}
for (x = ; x < n ; x++) {
for (y = ; y < n ; y++) {
printf ("%3d" , a[x][y]) ;
}
printf ("\n") ;
}
}
return ;
}