import java.io.*;
public class SnakeMatrix // 蛇形矩阵
{
public static void main(String[] args) throws IOException
{
new CarryOut().Execute();
}
}
class CarryOut
{
public void Execute() throws IOException
{
BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入一个数:");
int n=Integer.parseInt(ch.readLine()); // n == 4
int[][] nn=new int[n][n];//定义一个二维数组
int number=1;
int t=0;
int tt=1;
if(n==1)
{
System.out.print(1);
return ;
}
while(true)
{
for(int i=t;i<=n-tt;i++) // 上
{
nn[t][i]=number++;
}
for(int k=tt;k<n-tt;k++) // 右
{
nn[k][n-tt]=number++;
}
for(int l=n-tt;l>=t;l--) // 下
{
nn[n-tt][l]=number++;
}
for(int j=n-tt-1;j>t;j--) // 左
{
nn[j][t]=number++;
}
t++;
tt++;
if(t>=n/2)
{
break;
}
}
if(n%2!=0)
{
nn[t][t]=number;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(String.format("%-5d", nn[i][j]).toString());
}
System.out.println();
}
}
}
public class SnakeMatrix // 蛇形矩阵
{
public static void main(String[] args) throws IOException
{
new CarryOut().Execute();
}
}
class CarryOut
{
public void Execute() throws IOException
{
BufferedReader ch=new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入一个数:");
int n=Integer.parseInt(ch.readLine()); // n == 4
int[][] nn=new int[n][n];//定义一个二维数组
int number=1;
int t=0;
int tt=1;
if(n==1)
{
System.out.print(1);
return ;
}
while(true)
{
for(int i=t;i<=n-tt;i++) // 上
{
nn[t][i]=number++;
}
for(int k=tt;k<n-tt;k++) // 右
{
nn[k][n-tt]=number++;
}
for(int l=n-tt;l>=t;l--) // 下
{
nn[n-tt][l]=number++;
}
for(int j=n-tt-1;j>t;j--) // 左
{
nn[j][t]=number++;
}
t++;
tt++;
if(t>=n/2)
{
break;
}
}
if(n%2!=0)
{
nn[t][t]=number;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(String.format("%-5d", nn[i][j]).toString());
}
System.out.println();
}
}
}