蛇形填数-正方

#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

 

你可能感兴趣的:(蛇形填数-正方)