[腾讯]打印蛇形矩阵

/* 打印蛇形矩阵 */


#include<stdio.h> 
#define N 10
#define RIGHT 1
#define DOWN 2
#define LEFT 3
#define UP 4
int  a[N][N] = { 0 };
void printSqure()
{
    int i = 0, j = 0, flag = RIGHT, n = 1;
    while (1)
    {
        a[i][j] = n++;
        if (n > N*N)
        {
            break;
        }
        switch (flag)
        {
        case RIGHT:
        {
            if (j + 1 < N && !a[i][j + 1])
            {
                j++;
                break;
            }
            flag = DOWN;
        }
        case DOWN:
        {
            if (i + 1 < N && !a[i + 1][j])
            {
                i++;
                break;
            }
            flag = LEFT;
        }
        case LEFT:
        {
            if (j - 1 >= 0 && !a[i][j - 1])
            {
                j--;
                break;
            }
            flag = UP;
        }
        case UP:
        {
            if (i - 1 >= 0 && !a[i - 1][j])
            {
                i--;
                break;
            }
            flag = RIGHT;
        }
        default:
        {
            if (j + 1 < N && !a[i][j + 1])
            {
                j++;
                break;
            }
        }
        }
    }
    for (i = 0; i < N; i++)
    {
        printf("\n");
        for (j = 0; j < N; j++)
        {
            printf("%d\t", a[i][j]);
        }
    }

}

你可能感兴趣的:([腾讯]打印蛇形矩阵)