题解:
#include <stdio.h>
#include <string.h>
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int a[n][n];
memset(a,0,sizeof(a));//初始化数组a的值为0
int row=0,col=(n-1)/2;//变量用来保存行列的下标
for(int i=1;i<=n*n;i++){
a[row][col]=i;
int new_row=(row-1+n)%n;//更新行
int new_col=(col+1)%n;//更新列,下面判断是否符合要求
if(a[new_row][new_col]!=0){
row=(row+1)%n;
} else{
row=new_row;
col=new_col;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%4d",a[i][j]);//循环打印
}
printf("\n");
}
}
return 0;
}
结果:
标签:右上方,int,题解,scanf,memset,pta,120,include From: https://blog.csdn.net/2401_83605866/article/details/143464027