标签:
#include<stdio.h>//奇数阶魔方(734) int main() { int a[20][20],i,j,n,x,cnt; scanf("%d",&x); while(x--){ scanf("%d",&n); for(i=0;i<n;i++){ for(j=0;j<n;j++){ a[i][j]=0; } } a[0][n/2]=1; i=0;j=n/2; cnt=1; while(cnt<=n*n){ if(a[i][j]%n==0){ i=i+1; a[i][j]=++cnt; } else if(i==0){ i=n-1;j=j+1; a[i][j]=++cnt; } else if(j!=n-1&&a[i][j]%n!=0){ i=i-1;j=j+1; a[i][j]=++cnt; } else if(j==n-1&&i!=0){ i=i-1;j=0; a[i][j]=++cnt; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ printf("%d ",a[i][j]); } printf("\n"); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/minimalism/p/4537595.html