知识点:
- 偏移量的应用
题目分析:
题目代码:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int m[191][191]={0};
int main(){
int n,big;
cin>>n>>big;
int x=1;
int y=1;
int step = 1;
int p = 1;
for(int i=1;i<=n*big;i++){
m[x][y] = i;
int a = x+dx[step];
int b = y+dy[step];
if(a<1||a>=n+1||b<1||b>=big+1||m[a][b]){
step = (step+1)%4;
a = x+dx[step];
b = y+dy[step];
}
x = a;
y = b;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=big;j++){
printf("%d ",m[i][j]);
}
cout<<endl;
}
}