一道打印M的面试题

时间:2024-07-28 18:37:02
 public class Demo {

     /**
* 平面图形题(二维数组)
*/
public static void main(String[] args) { int num = 25; int hight = num/4+1;
int width = num; int arr[][] = new int[hight][width]; int x=hight-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>hight-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){
System.out.print(" ");
}else{
System.out.print(arr[i][j]);
}
}
System.out.println();
} } }