一大淼题,直接瞎搞即可,不过一定要仔细看题目给定的条件。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define M 50 5 using namespace std; 6 int a[M][M]={0}; 7 int main(){ 8 freopen("magic.in","r",stdin); 9 freopen("magic.out","w",stdout); 10 int n; cin>>n; 11 int lastx=1,lasty=1+(n>>1); a[lastx][lasty]=1; 12 for(int i=2;i<=n*n;i++){ 13 if(lastx==1&&lasty!=n){a[lastx=n][++lasty]=i; continue;} 14 if(lastx!=1&&lasty==n){a[--lastx][lasty=1]=i; continue;} 15 if(lastx==1&&lasty==n){a[++lastx][lasty]=i; continue;} 16 if(lastx!=1&&lasty!=n){ 17 if(a[lastx-1][lasty+1]==0) 18 a[--lastx][++lasty]=i; 19 else a[++lastx][lasty]=i; 20 continue; 21 } 22 } 23 for(int i=1;i<=n;i++){ 24 for(int j=1;j<=n;j++) printf("%d ",a[i][j]); 25 printf(" "); 26 } 27 }