自己写的魔方阵算法(只能输出奇数阶)
#include <stdio.h>
int main(int argc, char *argv[])
{
int line,col,n,p,i,j;
int a[15][15]={0};
p=1;
while(p==1)
{
scanf("%d",&n);
if(n%2!=0&&n<=507)
p=0;
}
a[0][(n-1)/2]=1;
line=0;
col=(n-1)/2;
for(p=2;p<=n*n;p++)
{
if(line==0&&col==n-1)
{
a[line+1][col]=p;
line=line+1;
col=col;
}
else if(line==0)
{
a[n-1][col+1]=p;
line=n-1;
col=col+1;
}
else if(col==n-1)
{
a[line-1][0]=p;
line=line-1;
col=0;
}
else if(a[line-1][col+1]!=0)
{
a[line+1][col]=p;
line=line+1;
col=col;
}
else
{
a[line-1][col+1]=p;
line=line-1;
col=col+1;
}
printf("a[%d][%d]=%d
",line,col,a[line][col]);
}
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("
");
}
return 0;
}