打印杨辉三角的前十行
发布网友
发布时间:2022-04-24 07:25
我来回答
共1个回答
热心网友
时间:2022-06-17 14:21
public class Yanghui {
public Yanghui(int level)
{
_level = level;
}
public void Print()
{
int plast, pcur;
int[] yangh = new int[((_level+1)*_level)/2];
yangh[0] = 1;
if(_level == 1)
return;
plast = 0;
pcur = 1;
for (int i=2; i<=_level; i++)
{
int j;
yangh[pcur] = 1;
yangh[pcur+i-1] = 1;
for (j=1; j<i-1; j++)
{
yangh[pcur+j] = yangh[plast+j-1]+yangh[plast+j];
}
plast = pcur;
pcur+=j+1;
}
pcur = 0;
for (int i=1; i<=_level; i++)
{
for (int j=0; j<i; j++)
{
System.out.print(yangh[pcur++]+" ");
}
System.out.println();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Yanghui yh = new Yanghui(10);
yh.Print();
}
private int _level;
}