样例输入:
4
样例输出:
10  11  12  1
9   16  13  2
8   15  14  3
7    6   5  4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int a[50][50];
    int i,j,x,y,cot,n;
    scanf("%d",&n);
    memset(a,0,sizeof(a));
    cot=a[0][n-1]=1;
    y=n-1,x=0;
    cot=2;
    while(cot<=n*n)
    {
        while(x+1<n&&(!a[x+1][y]))
        {
            a[++x][y]=cot++;
           // printf("1\n");
        }
        while(y-1>=0&&(!a[x][y-1]))
        {
            a[x][--y]=cot++;
            //printf("2\n");
        }
        while(x-1>=0&&(!a[x-1][y]))
        {
            a[--x][y]=cot++;
            //printf("3\n");
        }
        while(y+1<n&&(!a[x][y+1]))
        {
            a[x][++y]=cot++;
            //printf("4\n");
        }
    }
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
            printf("%3d",a[i][j]);
        printf("\n");
    }
    return 0;
}
posted on 2015-09-22 20:44  cnxo  阅读(131)  评论(0编辑  收藏  举报