代码
package day3;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// for (int i = 0; i < 10; i++) {
// for (int j = 0; j < 10; j++) {
// System.out.print("外:"+i+"内:"+j);
// }
// System.out.println();
// }
int n = 6;
int [][] arr = new int [n+1][n+1];
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j <=i; j++) {
if(i==1||i==j) {
arr[i][j]=1;
}else {
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
}
}
for (int i = 1; i < arr.length; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(arr[i][j]+",");
}
System.out.println();
}
}
}
i内层,j外层
核心:每个位置的值为(i-1,j)位置的值加(i-1,j-1)位置的值