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.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
using namespace std;
|
|
|
|
|
const int N = 110;
|
|
|
|
|
int dx[] = {0, 1, 0, -1}; // 右下左上
|
|
|
|
|
int dy[] = {1, 0, -1, 0}; // 右下左上
|
|
|
|
|
int a[N][N];
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
int n = 5, m = n * n;
|
|
|
|
|
|
|
|
|
|
int idx = 1;
|
|
|
|
|
int x = 0, y = 0, p = 0;
|
|
|
|
|
while (idx <= m) {
|
|
|
|
|
a[x][y] = idx; //
|
|
|
|
|
idx++; // 值在长大
|
|
|
|
|
// p是说方向数组中的游标,它是需要变化的,什么情况下变化呢?
|
|
|
|
|
// 1、出界了就变化
|
|
|
|
|
if (x + dx[p] >= n || y + dy[p] >= n || a[x + dx[p]][y + dy[p]])
|
|
|
|
|
p = (p + 1) % 4;
|
|
|
|
|
|
|
|
|
|
// 下一个坐标位置
|
|
|
|
|
x += dx[p], y += dy[p];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
for (int j = 0; j < n; j++)
|
|
|
|
|
printf("%3d", a[i][j]);
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|