c语言代码
#include<stdio.h>
int main(){
int n,m,turn=0,a=1,t=1;
scanf("%d",&n);
m = n;
int b[m][n];
turn = n/2;
if(n%2==1)
b[(m-1)/2][(n-1)/2] = n*m;
for(;a<=turn; a++){
for(int w=a-1; w<=n-a; w++){
b[a-1][w]=t;
t++;
}
for(int w=a; w<=m-a; w++){
b[w][n-a] = t;
t++;
}
for(int w=n-a-1; w>=a-1; w--){
b[m-a][w] = t;
t++;
}
for(int w=m-a-1; w>=a; w--){
b[w][a-1] = t;
t++;
}
}
for(int i=0; i<=m-1; i++){
for(int j=0; j<=n-1;j++){
if(j==n-1)
printf("%d\n",b[i][j]);
else
printf("%d ",b[i][j]);
}
}
}