1. 程式人生 > >JAVA 之楊輝三角實現

JAVA 之楊輝三角實現

public static void printYH(int n) {
int[][] arr = new int[n][];
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[i+1];
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
}
}
}
for (int[] j: arr) {
for (int i : j) {
System.out.print(i+" ");
}
System.out.println();
}
}