1.什么是杨辉三角
每一行头尾都为1,每个数都等于上面两个数之和
arr[3][1] = arr[2][0]+arr[2][1];
arr[3][2] = arr[2][1]+arr[2][2];
2.实现
int[][] arr = new int[10][];
//数组元素赋值
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[i+1]; //是i+1,i=0时第一行长度为1
arr[i][0] = 1; //头尾赋值为1
arr[i][i] = 1;
for (int j = 1; j < arr[i].length-1; j++) { //从第[1]列开始
//arr[i-1][j-1] 上一行前一列
//arr[i-1][j] 上一行本列
arr[i][j] = arr[i-1][j-1]+arr[i-1][j];
}
}
//遍历数组
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
标签:arr,Java,int,length,++,二维,数组,杨辉三角
From: https://www.cnblogs.com/zhishu/p/16614029.html