规律:
第一行中间数字为1,
从数字1开始,它的右上方位置填数字2,依次递增
第一行的上一行为最后一行,
最后一列的右侧一列为第一列,
若某个数字的右上方的位置已经填数字了,则它的下一个数字填在它的正下方的位置,
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
#define size 20
int cube[size][size]={0};
int main(){
ios::sync_with_stdio(false);
int t,n;
cin>>t;
while(t--){
memset(cube,0,sizeof(cube));
cin>>n;
cube[0][n/2]=1;
int x=0;
int y=n/2;
int num=1;
while(num<n*n){
--x;
++y;
++num;
if((x<0)&&(y>=n)){
x+=2;
--y;
}
if(x<0)
x=n-1;
if(y>=n)
y=0;
while(cube[x][y]!=0){
x+=2;
--y;
if(x<0)
x=n-1;
if(y>=n)
y=0;
}
cube[x][y]=num;
}
for(x=0;x<n;x++){
for(y=0;y<n;y++)
cout<<setw(4)<<right<<cube[x][y];
cout<<endl;
}
}
return 0;
}