数组存储蛇形数据

数组存储蛇形数据_第1张图片

#include 
#include 


#define N 10
void main()
{
    int a[N][N] = {0};

    int data = 1;
    for (int i = 0,j = 0, k = 0; k < (N+1)/2; k ++)
    {
        while(j<N - k)
        {
            a[i][j] = data;
            data ++;
            j++;
        }
        j--;
        i++;
        while (i<N - k)
        {
            a[i][j] = data;
            data++;
            i++;
        }
        i--;
        j--;
        while (j > k - 1)
        {
            a[i][j] = data;
            data++;
            j--;
        }
        j++;
        i--;
        while (i > k)
        {
            a[i][j] = data;
            i--;
            data++;
        }
        i++;
        j++;
    }


    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            printf("%3d",a[i][j]);
        }
        printf("\n");
    }

    system("pause");
}

你可能感兴趣的:(数据结构)