num=$var;}publicfunctiondisplay(){$n=$this->num;$arr=array();//$arr=array_fill(0,$n+1,array_fill(0,$n+1,0));$arr=array_fill(0,3,0);$arr=1;echostr_pad(" ",$n*12," ");printf("%3d",$arr);echo"
";for($i=2;$i<=$n;$i++){$arr[$i]=array_fill(0,($i+2),0);for($j=1;$j<=$i;$j++){if($j==1)echostr_pad(" ",($n+1-$i)*12," ");printf("%3d",$arr[$i][$j]=$arr[$i-1][$j-1]+$arr[$i-1][$j]);echo" ";}echo"
";}}}$yh=newT();//$yh=newT(数量);$yh->display();?>只输出数组的方式如下:1234567891011121314151617较为便捷,代码量较少的实现方式如下:1234567#-*-coding:utf-8-*-#!/usr/bin/envpythondeftriangles():L=whileTrue:yieldLL=[sum(i)foriinzip(+L,L+)]该方式用到了列表生成式,理解起来较困难,下面是另一种方式:12345678