/*输出 n=5 的螺旋方阵
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
*/
class H
{
public static void main(String[] args)
{
int n=5;
int [][]arr=new int[n][n];
int num=0;
int x=0;
int y=-1;
for (int d=0;d<n ;d++ )
{
num++;
y++;
arr[x][y]=num;
}
for (int i=0;i<n;i+=2 )
{
for (int a=1;a<n-i ;a++ )
{
x++;
num++;
arr[x][y]=num;
}
for (int b=1;b<n-i ;b++ )
{
y--;
num++;
arr[x][y]=num;
}
for (int c=1;c<n-i-1 ;c++ )
{
x--;
num++;
arr[x][y]=num;
}
for (int d=1;d<n-i-1 ;d++ )
{
y++;
num++;
arr[x][y]=num;
}
}
for(int i=0;i<n;i++)
{
for (int j=0;j<n;j++ )
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
|