public class PrintM {
/**
* 一道打印M的面试题
* +++++++++++++++++++++++++++
* 0 1 2 3 4 5 6 7 8
* ----------------------
* 0| 3 7
* 1| 2 4 6 8
* 2|1 5 9
* ++++++++++++++++++++++++++
* arr[2][0] = 1 ;
* arr[1][1] = 2 ;
* arr[0][2] = 3 ;
* arr[1][3] = 4 ;
* arr[2][4] = 5 ;
* arr[1][5] = 6 ;
* arr[0][6] = 7 ;
* arr[1][7] = 8 ;
* arr[2][8] = 9 ;
*
* 规律:
* x --> 2 1 0 1 2 1 0 1 2
* y --> 0 1 2 3 4 5 6 7 8
*/
public static void main(String[] args) {
int num = 45;
int height = num/4+1;
int width = num;
int arr[][]=new int[height][width];
int x = height - 1;
int y = 0;
boolean order = false;
for(int i=1; i<=num; i++){
arr[x][y] = i;
y++;
if(order==false){
x--;
}
if(order==true){
x++;
}
if(x<0){
order = true;
x = x + 2;
}
if(x>(height - 1)){
order = false;
x = x -2;
}
}
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
if(arr[i][j]==0){
if(height>9)
System.out.print(" ");
else
System.out.print(" ");
}
else
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}