文章
3
粉丝
40
获赞
1
访问
1.6k
#include<bits/stdc++.h>
using namespace std;
int res[25][25] = {0};
int main()
{
int n;
cin >> n ;
int x = 0,y = 0;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
for(int d = 0,k = 1;k <=n*n;k++)
{
res[x][y] = k;
int a = x + dx[d];
int b = y + dy[d];
if(a >= n || a < 0 || b >= n || b < 0 || res[a][b])
{
d = (1+d) % 4;
a = x + dx[d];
b = y + dy[d];
}
x = a,y = b;
}
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++)
{
printf("%-4d", res[i][j]);
}
printf("\n");
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发