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

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int M = 11 ;
 4 int a[M][M] ;
 5 int main ()
 6 {
 7     int n , x , y , tot = 0 ;
 8     while (~ scanf ("%d" , &n) ) {
 9         memset (a , 0 , sizeof(a)) ;
10         tot = a[x = 0][y = 0] = 1 ;
11         while (tot < n * n) {
12             while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot ;
13             while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot ;
14             while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot ;
15             while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot ;
16         }
17         for (x = 0 ; x < n ; x++) {
18             for (y = 0 ; y < n ; y++) {
19                 printf ("%3d" , a[x][y]) ;
20             }
21              printf ("
") ;
22         }
23     }
24     return 0 ;
25 }
View Code
原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4391680.html