package test;
public class YangHuiTriangle
{
int maxlevel=20;
//分配内存
int yangHui[][]=new int[maxlevel][];
public void countTriangle(int yangHui[][])
{
for(int i=0;i {
yangHui[i]=new int[i+1];
}
//计算杨辉三角
for (int i=1;i {
yangHui[0][0]=1;
yangHui[i][0]=1;
yangHui[i][yangHui[i].length-1]=1;
for(int j=1;j {
if(j>0&&j {
yangHui[i][j]=yangHui[i-1][j-1]+yangHui[i-1][j];
}
}
}
}
public void printTriangle(int yangHui[][])
{
for (int i=0;i {
for(int k=yangHui.length;k>i+1;k–)
{
System.out.print(” “);
}
for(int j=0;j {
System.out.print(yangHui[i][j]);
if(yangHui[i][j]>=1&&yangHui[i][j]<10)
{
System.out.print(” “);
}
else if(yangHui[i][j]<100)
{
System.out.print(” “);
}
else if(yangHui[i][j]<1000)
{
System.out.print(” “);
}
else if(yangHui[i][j]<10000)
{
System.out.print(” “);
}
else
{
System.out.print(” “);
}
}
System.out.println();
}
}
public void run()
{
countTriangle(yangHui);
printTriangle(yangHui);
}
public static void main(String[] args)
{
YangHuiTriangle triangle=new YangHuiTriangle();
triangle.run();
}
}