杨辉三角

//Triangle[i][j]=Triangle[i-1][j-1]+Triangle[i-1][j];
//杨辉三角
/*
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
*/
public class YangHui{
public static void main(String[] args)
{
int[][] yh=new int[10][];//动态创建一个二维数组


for(int i=0;i<yh.length;i++)
{
yh[i]=new int[i+1];

}
//初始化赋值
for(int i=0;i<yh.length;i++)
{
for(int j=0;j<yh[i].length;j++)
{
yh[i][0]=yh[i][i]=1;
if(i>0&&j>0&&j<i)
{
yh[i][j]=yh[i-1][j-1]+yh[i-1][j];
}
}
}
//遍历
for(int i=0;i<yh.length;i++)
{
for(int j=0;j<yh[i].length;j++)
{
System.out.print(yh[i][j]+" ");
}
System.out.println();
}


}

}

原文地址:https://www.cnblogs.com/steel-chen/p/6698025.html