使用二维数组打印出如下图的杨辉三角
代码如下:
public static void main(String[] args) {
int a [] [] = new int[10][];
int m =10;
for (int i = 0; i < a.length; i++) {
a[i] = new int[i+1];
}
for (int i = 0; i < a.length; i++) {
for (int j = 10-i; j >=0; j--) {
System.out.print("\t");
}
for (int j = 0; j < a[i].length; j++) {
if (j==0||j==a[i].length-1) {
a[i][j] = 1;
}else{
a[i][j] = a[i-1][j-1]+a[i-1][j];
}
System.out.print(a[i][j]+"\t\t");
}
System.out.println();
}
}
结果如下:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Process finished with exit code 0
最后,觉得有用的话,可以点赞、收藏,加关注哟,要不下次就找不见了哟!