编程语言
首页 > 编程语言> > c/c++蛇形矩阵源代码!

c/c++蛇形矩阵源代码!

作者:互联网

include<stdio.h>

include <stdlib.h>

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;//赋值
		j++;//往右边移动
		data++;
	}
	j--;//退回
	i++;//转向


	while (i<N - k)
	{
		a[i][j] = data;
		i++;
		data++;

	}
	i--;
	j--;//退回转向
	while (j >k - 1)//减去一层
	{
		a[i][j] = data;
		j--;
		data++;
	}
	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");

}

标签:++,c++,int,while,蛇形,printf,--,源代码,data
来源: https://www.cnblogs.com/zhmsos/p/14615037.html