java写杨辉三角形
public class YangHuiTriangle {// 杨辉三角形
public static void main(String[] args) {
int[][] yhsj = new int[10][];// 定义一个长度为10的二维数组
for (int i = 0; i < yhsj.length; i++) {
yhsj[i] = new int[i + 1];// 定义二维数组的行数,第几行就有几个数
for (int j = 0; j < yhsj[i].length; j++) {
yhsj[i][i] = 1;// 行列相等的位置都为1
yhsj[i][0] = 1;// 第一列都为1
if (i>1) {// 从第三行开始
for( j=1;j<i;j++) {// 行大于列的位置,用以下公式,比如第三行第三个数
yhsj[i][j] = yhsj[i - 1][j - 1] + yhsj[i - 1][j];
}// 以上公式表示从第三行开始的数等于 正上方 的数与 左上方 的数的和
}
}
}//下面两个for循环结构遍历二维数组
for (int i = 0; i < yhsj.length; i++) {// 输出杨辉三角形
for (int j = 0; j < yhsj[i].length; j++)
System.out.print(yhsj[i][j] + "\t");
System.out.println();// 换行
}
}
}