题解
2026-11-19 16:44:15
发布于:浙江
3阅读
0回复
0点赞
欢迎点赞

#include<bits/stdc++.h>
using namespace std;
int main(){
int n ,x ,y ,h;
cin >> n;
int ls[n][n] = {};
h = ls[x = 0][y = n - 1] = 1;
while (h < pow(n ,2)){
while (x + 1 < n && !ls[x + 1][y])
ls[++x][y] = ++h;
while (y - 1 >= 0 && !ls[x][y - 1])
ls[x][--y] = ++h;
while (x - 1 >= 0 && !ls[x - 1][y])
ls[--x][y] = ++h;
while (y + 1 < n && !ls[x][y + 1])
ls[x][++y] = ++h;
}
for (int i = n - 1 ; i >= 0 ; i--){
for (int j = 0; j < n ; j++){
cout << ls[j][i] << " ";
}
cout << "\n";
}
return 0;
}
这里空空如也







有帮助,赞一个