//输出魔方阵 # include<stdio.h> # include<math.h> int a[15][15]; int n,i,j,p,k=1; int main() { p=1; while(p==1) { printf("请输入n:"); scanf("%d",&n); if(n>0&&n<=15&&n%2!=0) p=0; } for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=0; i=0; j=n/2; a[i][j]=k++; while(k<=pow(n,2)) { i--; j++; if(i<0&&j>n-1) { i+=2; j--; } else { if(i<0) i=n-1; if(j>n-1) j=0; } if(a[i][j]==0) a[i][j]=k++; else { i+=2; j--; a[i][j]=k++; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%-5d",a[i][j]); printf(" "); } return 0; }