喵。
2024-10-31 12:10:52
发布于:广东
0阅读
0回复
0点赞
#include <stdio.h>
int main(){
int n, i = 0, j = 0, a[120][120] = {0}, tot = 0;
scanf("%d", &n);
int num = a[0][0] = 1;
while (num < n*n){
while (j+1 < n && !a[i][j + 1]){
a[i][++j] = ++num;
}
while (i +1 < n && !a[i + 1][j]){
a[++i][j] = ++num;
}
while (j-1 >= 0 && !a[i][j - 1]){
a[i][--j] = ++num;
}
while (i -1>= 0 && !a[i - 1][j]){
a[--i][j] = ++num;
}
}
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
if(j==n-1){
printf("%d", a[i][j]);
}else{
printf("%d ", a[i][j]);
}
}
printf("\n");
}
return 0;
}
这里空空如也
有帮助,赞一个