#include<stdio.h>
int main(){
int n,temp;
printf("请输入幻方的节数:");
scanf("%d",&n);
int i=0,j=n/2,arr[20][20]={0};//
arr[i][j]={1};
for(temp=2;temp<=n*n;temp++){
i--;
j++;
if((i<0)&&(j<n))
i=n-1;
if((j>=n)&&(i>=0))
j=0;
if((i<0)&&(j>=n)){
i+=2;
j-=1;
}
if(arr[i][j]!=0){
i+=2;
j-=1;
arr[i][j]=temp;
}
else{
arr[i][j] =temp;
}
}
int flag =0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
flag++;
printf("%3d ",arr[i][j]);
if(flag%n==0){
printf("\n");
}
}}
return 0;
}
08-10
3384
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)