杨辉三角

package array.test;

public class Test2 {
public static void main(String[] args) {
//1. 声明并初始化二维数组
int[][] arr = new int[10][];

//2.给数组元素赋值
for (int i = 0;i < arr.length;i++){
arr[i] = new int[i+1];

//2.1给首末元素赋值
arr[i][0] = arr[i][i] = 1;

//2.2给每行的非首末元素赋值
if(i > 1){
for(int j = 1;j < arr[i].length-1;j++){
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
}

//3.遍历二维数组
for (int i = 0;i < arr.length;i++){
for (int j = 0;j < arr[i].length;j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
posted @ 2021-05-26 12:17  苏六来了✨  阅读(71)  评论(0编辑  收藏  举报