今天在群里聊天的时候看到这么一道笔试题,题目大意如下
编程实现
1
5 2
8 6 3
10 9 7 4
本人写的代码如下:
@Test public void test2() { int num = 1; //定义行数 final int scope = 6; int[][] Array = new int[scope][scope]; for (int i = 0; i < scope; i++) { int temp = scope; Array[i][i] = num++; for (int j = i + 1; j < scope; j++) { Array[j][i] = Array[j - 1][i] + temp--; } } for (int i = 0; i < scope; i++) { for (int j = 0; j < i + 1; j++) { System.out.print(Array[i][j] + " "); } System.out.println(); } }
浙公网安备 33010602011771号