-
二维数组树出杨辉三角:打印杨辉三角形(行数可以键盘录入)
Scanner scanner = new Scanner(System.in);
System.out.println("请输入行数");
int row = scanner.nextInt();
int[][] array = new int[row][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[i + 1];
array[i][0] = 1;
array[i][i] = 1;
if (i > 1) {
for (int j = 1; j <= i - 1; j++) {
array[i][j] = array[i - 1][j] + array[i - 1][j - 1];
}
}
}
for (int x = 0; x < array.length; x++) {
for (int v = 0; v < array[x].length; v++) {
System.out.print(array[x][v] + " ");
}
System.out.println();
}
网友评论