NB
2023-12-24 13:22:56
发布于:广东
50阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
typedef long long ll;
ll n, a[100][100], y = -1, x;
int main(){
cin >> n;
int cnt = 1;
while(cnt <= n * n){
while(y + 1 < n and a[x][y + 1] == 0){
a[x][++y] = cnt++;
}
while(x + 1 < n and a[x + 1][y] == 0){
a[++x][y] = cnt++;
}
while(y - 1 >= 0 and a[x][y - 1] == 0){
a[x][--y] = cnt++;
}
while(x - 1 >= 0 and a[x - 1][y] == 0){
a[--x][y] = cnt++;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个