https://www.acwing.com/problem/content/758/
模拟题
1 #include<iostream> 2 using namespace std; 3 const int N=110; 4 int a[N][N]; 5 int u[]={-1,0,1,0}; 6 int v[]={0,1,0,-1}; 7 int main(void){ 8 int n,m; 9 cin>>n>>m; 10 int x=0,y=0,d=1; 11 for(int i=1;i<=n*m;i++){ 12 a[x][y]=i; 13 int tx=x+u[d]; 14 int ty=y+v[d]; 15 if(tx<0||tx>=n||ty<0||ty>=m||a[tx][ty]){ 16 d=(d+1)%4; 17 tx=x+u[d]; 18 ty=y+v[d]; 19 } 20 x=tx,y=ty; 21 } 22 for(int i=0;i<n;i++){ 23 for(int j=0;j<m;j++){ 24 cout<<a[i][j]<<" "; 25 } 26 cout<<endl; 27 } 28 return 0; 29 }