C++ 代码
#include<cstdio>
int a[110][110] ={0};
int main(){
int N,M,x,y;
scanf("%d%d",&N,&M);
int cur=a[x=0][y=0] =1;
while(cur < N*M){
while(y+1<M && !a[x][y+1]) a[x][++y] =++cur;
while(x+1 <N && !a[x+1][y]) a[++x][y] = ++cur;
while(y-1 >=0 &&!a[x][y-1]) a[x][--y] = ++cur;
while(x-1 >=0&&!a[x-1][y]) a[--x][y] = ++cur;
}
for(int i =0;i <N;i++){
for(int j =0;j<M;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}