/**
* @see 打印出杨辉三角形(这是用的多维数组的形式,也可以根据公式计算),输出样式已经进行了调整
*/
class YangHuiSanJiao {
public static void main(String[] args) {
System.out.println("请输入正整数");
Scanner s = new Scanner(System.in);
String input = s.next();
int number;
try {
number = Integer.parseInt(input);
} catch (Exception e) {
System.out.println("您输入的不是整数");
return ;
}
if (number <= 0) {
System.out.println("您输入的不是正整数");
return ;
}
int length = number * 2 -1; //第二维数组的长度
long[][] array = new long[number][length]; //已经默认赋值为0
//第一行处理
array[0][length / 2] = 1;
//i为当前打印的行数,在数组中的表示为i - 1
for (int i = 2; i <= array.length; i++) {
for (int j = 0; j < length; j++) {
if (j - 1 < 0) { //第一个位置
array[i - 1][j] = array[i - 2][j + 1];
continue ;
}
if (j + 1 >= length) { //最后一个位置
array[i - 1][j] = array[i - 2][j - 1];
continue ;
}
if (array[i - 2][j - 1] > 0 || array[i - 2][j + 1] > 0) { //有数字出现的位置
array[i - 1][j] = array[i - 2][j - 1] + array[i - 2][j + 1];
continue ;
}
}
}
//获取数组中的最大值