男人就是要写代码!
#include <iostream>
using namespace std;
int main()
{
int n,m,x = 0,y = 0,d,num;
cin >> m >> n;
int dx[4] = {0,1,0,-1},dy[4] = {1,0,-1,0};
int a[120][120] = {};
for(x = 1,y = 1,d = 0,num = 1;num <= n*m;num++)
{
if(a[x][y] == 0)
a[x][y] = num;
if(x+dx[d] < 1 || y+dy[d] < 1 ||x+dx[d] > m || y+dy[d] > n||a[x+dx[d]][y+dy[d]] != 0)//越共探头查一下下一次有没有出界
{
d++;
if(d > 3)
d = d%3 - 1;
}
x+=dx[d];
y+=dy[d];
}
for(int i = 1;i <=m;i++)
{
for(int j = 1;j <= n;j++ )
{
cout << a[i][j] << ' ';
}
cout << endl;
}
return 0;
}