You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
720 B
32 lines
720 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 110;
|
|
int a[N][N];
|
|
|
|
//占位+右下左上
|
|
int dx[] = {0, 1, 0, -1};
|
|
int dy[] = {1, 0, -1, 0};
|
|
|
|
int main() {
|
|
int n;
|
|
cin >> n;
|
|
int x = 0, y = 0;
|
|
a[x][y] = 1;
|
|
|
|
int dir = 0; //方向
|
|
for (int i = 2; i <= n * n; i++) {
|
|
while (x + dx[dir] == n || x + dx[dir] < 0 || y + dy[dir] == n || y + dy[dir] < 0 ||
|
|
a[x + dx[dir]][y + dy[dir]] > 0) {
|
|
dir = (dir + 1) % 4;
|
|
}
|
|
x += dx[dir], y += dy[dir], a[x][y] = i;
|
|
}
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < n; j++)
|
|
printf("%3d", a[i][j]);
|
|
printf("\n");
|
|
}
|
|
return 0;
|
|
} |