题解
2024-04-13 20:07:04
发布于:广东
28阅读
0回复
0点赞
#include <iostream>
using namespace std;
int pz[110][110];
int main() {
int n,x = 0,y = -1;
cin >> n;
int cnt = 1;
while(cnt <= n * n) {
while(y+1 < n and pz[x][y+1] == 0) {
pz[x][++y] = cnt++;
}
while(x+1 < n and pz[x+1][y] == 0) {
pz[++x][y] = cnt++;
}
while(y-1 >= 0 and pz[x][y-1] == 0) {
pz[x][--y] = cnt++;
}
while(x-1 >= 0 and pz[x-1][y] == 0) {
pz[--x][y] = cnt++;
}
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++) {
cout << pz[i][j] << " ";
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个