杨辉三角规律:
- 第一行有1个元素,第n行有n个元素;//列数不确定的二维数组
- 每一行的每一个元素和最后一个元素都是1;
- 从第三行开始,对于非第一个元素和最后一个元素的元素的值,
arr[i][j] = arr[i-1][j] + arr[i-1][j-1]
java
//2024.07.03
public class YangHui {
public static void main(String[] args) {
int[][] yangHui = new int[10][];
for (int i = 0; i < yangHui.length; i++) {
yangHui[i] = new int[i + 1];
for (int j = 0; j < yangHui[i].length; j++) {
if (j == 0 || j == yangHui[i].length - 1) {
yangHui[i][j] = 1;
}else{
yangHui[i][j] = yangHui[i-1][j] + yangHui[i-1][j-1];
}
}
}
for (int i = 0; i < yangHui.length; i++) {
for (int j = 0; j < yangHui[i].length; j++) {
System.out.print(yangHui[i][j] + "\t");//遍历输出
}
System.out.println();
}
}
}