- chenxueze's blog
蛇形填数
- 2024-12-24 18:05:06 @
蛇形填数
#include<bits/stdc++.h>
using namespace std;
int cnt,b = 1,t = 1,a[21][21];
int main() {
int n;
cin >> n;
cnt = n;
while (true) {
if (n == 0) {
break;
}
for (int i = t;i <= n; i++) {
a[i][n] = b++;
}
for (int i = n - 1;i >= t; i--) {
a[n][i] = b++;
}
for (int i = n - 1;i >= t; i--) {
a[i][t] = b++;
}
for (int i = t + 1;i <= n - 1; i++)
{
a[t][i] = b++;
}
n--;
t++;
}
for (int i = 1;i <= cnt; i++) {
for (int j = 1;j <= cnt; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}