文章
37
粉丝
1
获赞
79
访问
2.8k
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int main()
{
int n;
cin >> n;
int res[20][20] = { 0 };
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
for (int x = 0, y = 0, d = 0, k = 1; k <= n * n; k++){
res[x][y] = k;
int a = x + dx[d], b = y + dy[d];
if (a < 0 || a >= n || b < 0 || b >= n || res[a][b]) {
d = (d + 1) % 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]);
cout << endl;
}
return 0;
}
登录后发布评论
暂无评论,来抢沙发