#include<stdio.h> int main() { int data[40][40]={0}; int N; scanf("%d",&N); int k=1; int i,j,m,n; i = 0; j = N/2; data[i][j] = k; m=i,n=j; // k-1 的行和列 k++; while(k<=N*N) { if(m==0&&n!=N-1){data[N-1][n+1] = k;m=N-1,n=n+1;} else if(m!=0&&n==N-1){data[m-1][0] = k;m=m-1,n=0;} else if(m==0&&n==N-1) {data[m+1][n] = k;m=m+1,n=n;} else if(m!=0&&n!=N-1&&data[m-1][n+1]==0){data[m-1][n+1]=k;m=m-1,n=n+1;} else{data[m+1][n] = k;m=m+1,n=n;} k++; } for(i=0;i<N;i++) { for(j=0;j<N;j++) { printf("%d ",data[i][j]); } printf("\n"); } return 0; }
标签:int,幻方,40,C语言,data,神奇 From: https://www.cnblogs.com/kmxojer/p/16749388.html