package 第三章;
import java.util.Scanner;
public class 蛇形填数 {
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[][]=new int[n][n];
int x=0,y=n-1;
int t=1;
a[0][n-1]=t;
while(t<n*n) {
while(x+1<=n-1&&a[x+1][y]==0)
{
a[++x][y]=++t;
}
while(y-1>=0&&a[x][y-1]==0)
{
a[x][--y]=++t;
}
while(x-1>=0&&a[x-1][y]==0)
{
a[--x][y]=++t;
}
while(y+1<=n-1&&a[x][y+1]==0)
{
a[x][++y]=++t;
}
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
System.out.printf("%3d",a[x][y]);
}
System.out.println();
}
}
}
import java.util.Scanner;
public class 蛇形填数 {
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[][]=new int[n][n];
int x=0,y=n-1;
int t=1;
a[0][n-1]=t;
while(t<n*n) {
while(x+1<=n-1&&a[x+1][y]==0)
{
a[++x][y]=++t;
}
while(y-1>=0&&a[x][y-1]==0)
{
a[x][--y]=++t;
}
while(x-1>=0&&a[x-1][y]==0)
{
a[--x][y]=++t;
}
while(y+1<=n-1&&a[x][y+1]==0)
{
a[x][++y]=++t;
}
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
System.out.printf("%3d",a[x][y]);
}
System.out.println();
}
}
}