#include
#include
#define N 5 /*确定魔方阵的大小*/
main()
{
long a[N][N]={0};
int count = 1, i = 0, j = (N-1)/2;
while (count <= N * N)
{
a[i][j] = count;
i--; /*指定下一个数*/
j++;
if(i < 0 && j <= N - 1) /*i和j范围可能出现的异常情况*/
i = N - 1;
else if(i >= 0 && j > N - 1)
j = 0;
else if(a[i][j] || (i < 0 && j > N - 1) )
{i = i + 2; j = j - 1;}
count++;
}
for (i = 0; i < N; i++) /*输出魔方阵*/
{
for (j = 0; j < N; j++)
{
printf("%8d", a[i][j]);
}
printf("\n\n");
}
system("pause");
}
阅读(1564) | 评论(0) | 转发(0) |