打印魔方阵

92 阅读1分钟

本文已参与新人创作礼活动,一起开启掘金创作之路

#include <stdio.h> #include <conio.h> #include <time.h> #include <stdlib.h> #include <math.h> #include <windows.h>

//void mySleep(clock_t wait); #include <conio.h> #include <ctype.h>

#include <stdio.h> #include <conio.h> #include <time.h> #include <stdlib.h> #include <math.h> #include <windows.h>

#define POSCOUNT 2

#include <stdio.h>

//宏定义,定义螺旋矩阵的阶数,要想改变数组的阶数,只需在改变N的值即可

#include <stdio.h>

#define N 7

int main() { int row = 0; //行号 int line = N/2; //列号 int n = 1; //计数 int arr[N][N] = {0}; //定义数组存放元素并初始化

//生成“魔方阵”
for(n = 1; n <= N*N; n++)
{
	arr[row][line] = n;
	if( 0 == n%N)
	{
		row = (row+1) % N;	//行号 +1
	}
	else
	{
		row = ((row-1)+N) % N; //行号 -1
		line  = (line +1) % N; //列号 +1
	}
}

//输出“魔方阵”
for(row = 0; row < N; row++)
{
	for(line = 0; line < N; line++)
	{
		printf("%3d", arr[row][line]);
	}
	printf("\n");
}

return 0;

}