蛇形走法

蛇形走法,这儿实现了如下的走法

1 8 7
2 9 6
3 4 5


/*
1 8 7
2 9 6
3 4 5
*/

#include
#include

#define N 5

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


	int data = 1;
	for (int i = 0, j = 0, k = 0; k < (N + 1) / 2; k++) //控制层数
	{
		//往下走
		while (ik-1)
		{		
			a[i][j] = data;
			data++;
			i--;
		}
			
		//往左走
		i++;
		j--;
		while (j>i)
		{
			
			a[i][j] = data;
			data++;
			j--;
		}
		j++;
		i++;
	}



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

	system("pause");
}


你可能感兴趣的:(C语言)