螺旋方阵-sdut

螺旋方阵

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

n×n的螺旋方阵当n=5和n=3时分别是如下的形式

 
请给出一个程序,对于任意的输入n(0<n<11),输出按照上面规律所获得的n×n的螺旋方阵。

Input

输入第一行为整数m(0<m<10),代表有m组输入;
接下来是m行数据,每行输入一个n(0<n<11)。

Output

按照输入的次序,依次输出每一个n×n方阵(一个方阵的同一行数据之间以'\t'分隔)
两个输出方阵之间输出一个空行。

Example Input

1
4

Example Output

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


实验示例

#include <stdio.h>
#include <memory.h>
int n=1;

int s[13][13]={0};
void list( int a, int b, int c, int d)
{
    if(a>c)
    {
        return ;
    }
    int i, j;
    for(j=b;j<d;j++)
    {
        s[a][j]=n;
        n++;
    }
    for(i=a;i<c;i++)
    {
        s[i][d]=n;
        n++;
    }
    for(j=d;j>b;j--)
    {
        s[c][j]=n;
        n++;
    }
    for(j=c;j>a;j--)
    {
        s[j][b]=n;
        n++;
    }
    list( ++a, ++b, --c, --d);
}


int main()
{
    int i=1, j, l, k;
    memset(s, 0, sizeof(s));
    scanf("%d",&l);
    while(l--)
    {
        scanf("%d",&k);
        list( 1, 1, k, k);
        if(k%2!=0)
        {
            s[k/2+1][k/2+1]=k*k;
        }
        for(i=1;i<=k;i++)
        {
            for(j=1;j<k;j++)
            {
                printf("%d\t",s[i][j]);
            }
            printf("%d\n", s[i][j]);
        }
        n=1;
        if(l>=1)
        {
            printf("\n");
        }
    }
    return 0;
}

posted on 2017-04-06 14:38  ouon.cc  阅读(191)  评论(0编辑  收藏  举报