如下图所示,代码实现蛇形数组的打印。
代码如下所示:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define N 4
void DisPlay(int arr[N][N]) //显示整个数组
{
int i = 0;
int j = 0;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
printf("%3d ",arr[i][j]);
}
printf("\n");
}
printf("\n");
}
void Clockwise(int arr[N][N]) //顺时针依次增加(对应左上角的图)
{
int number = (N+1)/2;
int i = 0;
int j = 0;
int k = 1;
int counter = 0;
for(counter = 0;counter < number;counter++)
{
while(j<N-counter)
{
arr[i][j] = k++;
j++;
}
i++;
j--;
while(i<N-counter)
{
arr[i][j] = k++;
i++;
}
i--;
j--;
while(j>=counter)
{
arr[i][j] = k++;
j--;
}
i--;