螺旋矩阵


#include <stdio.h>

#define MAX    6
#define UP     1
#define DOWN   2
#define RIGHT  3
#define LEFT   4

int main()
{
    int Test[MAX][MAX];
    int i,j,Count=0;
    int Direction=UP;
    int Row=0,Col=0,ReverseRow=MAX-1,ReverseCol=MAX-1;

    for(i=0; i<MAX; i++)
    {
        for(j=0; j<MAX; j++)
        {
            Test[i][j]=0;
        }
    }

    while(Count<MAX*MAX+1)
    {
        switch(Direction)
        {
        case UP:
            {
                i=Row++;
                for(j=0; j<MAX-1; j++)
                {
                    if(Test[i][j]==0)
                    Test[i][j]=Count++;
                }
                Direction=RIGHT;
                break;
            }
        case RIGHT:
            {
                j=ReverseCol--;
                for(i=0; i<MAX; i++)
                {
                    if(Test[i][j]==0)
                    Test[i][j]=Count++;
                }
                Direction=DOWN;
                break;
            }
        case DOWN:
            {
                i=ReverseRow--;
                for(j=MAX-1-1; j>0; j--)
                {
                    if(Test[i][j]==0)
                    Test[i][j]=Count++;
                }
                Direction=LEFT;
                break;
            }
        case LEFT:
            {
                j=Col++;
                for(i=MAX-1; i>0; i--)
                {
                    if(Test[i][j]==0)
                    Test[i][j]=Count++;
                }
                Direction=UP;
                break;
            }
        default:
            {
                break;
            }

        }
    }

    for(i=0; i<MAX; i++)
    {
        for(j=0; j<MAX; j++)
        {
            printf("%d\t",Test[i][j]);
        }
        printf("\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值