面试题(平面图形题 用二维数组解决)

package cn.itcast.demo;

import org.junit.Test;

//面试题
public class demo1 {
/*
*
*            3       7
*          2  4   6   8
*        1      5        9
*

*     arr[2][0]

*     arr[1][1]

*     arr[0][2]

*     arr[1][3]

*     arr[2][4]

*     arr[1][5]

*     arr[0][6]

*     arr[1][7]

*     arr[2][8]


* 平面图形题 用二维数组解决
*/
@Test
public void test() {
int num = 9;
int arr[][] = new int[3][9];
int x = 2;
int y = 0;
boolean order = false;
for (int i = 1; i <= 9; i++) {
arr[x][y] = i;
y++;

if (order)
x++;
else
x--;
if (x < 0) {
order = true;
x = x + 2;
}
if (x > 2) {
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();
}
}

}

原文地址:https://www.cnblogs.com/xiaohuihui123/p/4359076.html