package com.dahongcui.java;
/*
* 打印一个10行的杨辉三角
* 杨辉三角构造特点:第一行有一个元素,第n行有n个元素
* 每一行的第一个和最后一个元素都是1
* 符合yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
*/
public class YangHuiTest {
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];
yangHui[i][0] = 1;
yangHui[i][i] = 1;
for (int j = 1; j < yangHui[i].length - 1; j++) {
yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
}
}
for (int i = 0; i < yangHui.length; i++) {
for (int j = 0; j < yangHui[i].length; j++) {
System.out.print(yangHui[i][j] + " ");
}
System.out.println();
}
}
}
原文:https://www.cnblogs.com/nxnxxn/p/14037335.html