题目

代码1
public class YangHuiTriangle {public static void main(String[] args) {print(10);}public static void print(int num) {int[][] arr = new int[num][];for (int i = 0; i < num; i++) {
arr[i] = new int[i + 1];}for (int i = 0; i < arr.length; i++) {for (int j = 0; j < arr[i].length; j++) {if (j == 0 || i == j) {
arr[i][j] = 1;} else {arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];}}}for (int[] a :arr) {System.out.println(Arrays.toString(a));}}
}
代码2
public class YangHuiTriangle2 {public static void main(String[] args) {print(10);}public static void print(int num) {int[][] arr = new int[num][];for (int i = 0; i < arr.length; i++) {arr[i] = new int[i + 1];
arr[i][0] = arr[i][i] = 1;
for (int j = 1; j < arr[i].length - 1; j++) {arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];}}for (int[] a :arr) {System.out.println(Arrays.toString(a));}}
}