Javapublic class TriangleArray{ public static void main(String[] args) { final int NMAX = 10; // allocate triangular array int[][] odds = new int[NMAX + 1][]; for (int n = 0; n <= NMAX; n++) odds[n] = new int[n + 1]; // fill triangular array for (int n = 0; n < odds.length; n++) for (int k = 0; k < odds[n].length; k++) { /* * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k) */ int lotteryOdds = 1; for (int i = 1; i <= k; i++) lotteryOdds = lotteryOdds * (n - i + 1) / i; odds[n][k] = lotteryOdds; } // print triangular array for (int[] row : odds) { for (int odd : row) System.out.printf("%4d", odd); System.out.println(); } }} PHP<?php/* 默认输出十行 , 用T(值)的形式可改变输出行数 */class T{ private $num; public function __construct($var=10) { if ($var<3) die("值太小啦!"); $this->num=$var; } public function display(){ $n=$this->num; $arr=array(); //$arr=array_fill(0,$n+1,array_fill(0,$n+1,0)); $arr[1]=array_fill(0,3,0); $arr[1][1]=1; echo str_pad(" ",$n*12," "); printf("%3d",$arr[1][1]); echo "<br/>"; for($i=2;$i<=$n;$i++){ $arr[$i]=array_fill(0,($i+2),0); for($j=1;$j<=$i;$j++){ if($j==1) echo str_pad(" ",($n+1-$i)*12," "); printf("%3d",$arr[$i][$j]=$arr[$i-1][$j-1]+$arr[$i-1][$j]); echo " "; } echo"<br/>"; } }}$yh=new T(); //$yh=new T(数量);$yh->display();?>只输出数组的方式如下:<?php$max = 10;$L = [1];var_dump($L);$L = [1,1];var_dump($L);$n = 2;while ($n <= $max - 1){ $oldL = $L; $L[$n] = 1; $n++; for ($i = 1;$i <count($oldL);$i++){ $L[$i] = $oldL[$i-1] + $oldL[$i]; } var_dump($L);}?>