蛇形填数-正方

#include <stdio.h>
#include <string.h>
#define M 101
int s[M][M];

int main()
{
    int n, i, j, t;
    while(scanf("%d", &n) != EOF){

        memset(s, 0, sizeof(s));
        t = 1;    
        s[i=1][j=n] = t++;
        while(t <= n*n){
            
            while(i+1 <= n && !s[i+1][j]){

                    s[++i][j] = t++;

            }
            while(j-1 >= 1 && !s[i][j-1]){

                    s[i][--j] = t++;
            
            }
            while(i-1 >= 1 && !s[i-1][j]){

                    s[--i][j] = t++;

            }
            while(j+1 <= n && !s[i][j+1]){

                    s[i][++j] = t++;

            }
            
        }
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j < n; j++)
            {
                printf("%d ", s[i][j]);
            }

            printf("%d\n", s[i][j]);
        }
    }
    return 0;
}
View Code

 

posted @ 2013-08-17 10:52  心中的阿哲  阅读(157)  评论(0编辑  收藏  举报