网上有很多算法..随便搜一下就有了!
[CODE]# include<stdio.h>
main()
{
int m[16][16];
int i,j,k,n,p;
p=1;
while(p==1)
{
printf("please input the N (1-15)\n");
scanf("%d",&n);
if((n!=0)&&(n<=15)&&(n%2!=0))
p=0;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
m[i][j]=0;
j=n/2+1;
m[1][j]=1;
for(k=2;k<=n*n;k++)
{
i=i-1;
j=j+1;
if((i<1)&&(j>n))
{
i=i+2;
j=j-1;
}
else
{
if(i<1)
i=n;
if(j>n)
j=1;
}
if(m[i][j]==0)
m[i][j]=k;
else
{
i=i+2;
j=j-1;
m[i][j]=k;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%4d",m[i][j]);
printf("\n");
}
printf("pleasa press any key to quit");
getch();
}[/CODE]