(感觉自己并没有什么写题解的必要啊……做点补充好了,顺便扔代码
D1T1.神奇的幻方
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,a[41][41]; 6 int main() 7 { 8 scanf("%d",&n); 9 int mid=n/2+1,x=1,y=mid; 10 a[1][mid]=1; 11 for(int i=2;i<=n*n;i++) 12 { 13 if(x==1&&y!=n)x=n,y++; 14 else if(x!=1&&y==n)x--,y=1; 15 else if(x==1&&y==n)x++; 16 else 17 { 18 if(a[x-1][y+1])x++; 19 else x--,y++; 20 } 21 a[x][y]=i; 22 } 23 for(int i=1;i<=n;i++) 24 { 25 for(int j=1;j<=n;j++) 26 printf("%d ",a[i][j]); 27 printf("\n"); 28 } 29 return 0; 30 }