package targetOffer;
public class Print_20 {
public static void printArray(int a[][],int col,int row)
{
if(a==null||col<=0||row<=0)
return;
int start=0;
while(col>start*2&&row>start*2){
printArrayInCircle(a,col,row,start);
start++;
}
}
public static void printArrayInCircle(int [][]a,int col,int row,int start)
{
int endX=col-1-start;
int endY=row-1-start;
for(int i=start;i<=endX;++i)
{
int number=a[start][i];
System.out.print(number+"\t");
}
if(start<endY)
{
for(int i=start+1;i<=endY;++i)
{
int number=a[i][endX];
System.out.print(number+"\t");
}
}
if(start<endX&&start<endY)
{
for(int i=endX-1;i>=start;--i)
{
int m=a[endY][i];
System.out.print(m+"\t");
}
}
if(start<endX&&start<endY-1)
{
for(int i=endY-1;i>=start+1;--i)
{
int m=a[i][start];
System.out.print(m+"\t");
}
}
System.out.println();
}
static void print(int a[][])
{
int length=a.length;
for(int i=0;i<length;i++)
{
int tt=a[i].length;
for(int j=0;j<tt;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("\n");
}
static void Test(int columns, int rows)
{
System.out.printf("Test Begin: %d columns, %d rows.\n", columns, rows);
if(columns < 1 || rows < 1)
return;
int[][]numbers = new int[rows][];
for(int i = 0; i < rows; ++i)
{
numbers[i] = new int[columns];
for(int j = 0; j < columns; ++j)
{
numbers[i][j] = i * columns + j + 1;
}
}
print(numbers);
printArray(numbers, columns, rows);
System.out.printf("\n");
numbers=null;
}
public static void main(String args[])
{
Test(5,7);
}
}
结果:
Test Begin: 5 columns, 7 rows.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
1 2 3 4 5 10 15 20 25 30 35 34 33 32 31 26 21 16 11 6
7 8 9 14 19 24 29 28 27 22 17 12
13 18 23