PHP 帕斯卡(Pascal)三角形

</pre>帕斯卡(Pascal)三角形基本上就是在解 nCr ,因为三角形上的每一个数字各对应一个n<big>
<span style="font-size:14px">C</span></big>r,其中 n 为 row,而 r 为 column,如下:    0<big><span style="font-size:14px">C</span></big>0   1<big><span style="font-size:14px">C</span></big>0 1<big><span style="font-size:14px">C</span></big>1  2<big><span style="font-size:14px">C</span></big>0 2<big><span style="font-size:14px">C</span></big>1 2<big><span style="font-size:14px">C</span></big>2 3<big><span style="font-size:14px">C</span></big>0 3<big><span style="font-size:14px">C</span></big>1 3<big><span style="font-size:14px">C</span></big>2 3<big><span style="font-size:14px">C</span></big>34<big><span style="font-size:14px">C</span></big>0 4<big><span style="font-size:14px">C</span></big>1 4<big><span style="font-size:14px">C</span></big>2 4<big><span style="font-size:14px">C</span></big>3 4<big><span style="font-size:14px">C</span></big>4<p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"> </p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px">对应的数据如下图所示:</p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"><img src="http://my.csdn.net/uploads/201208/15/1344996089_3343.jpg" border="1" alt="" /></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.200000762939453px"></p>
<pre code_snippet_id="242963" snippet_file_name="blog_20140318_1_8922135" name="code" class="php">
define ("N", 12);

function combi($n, $r){
    $p = 1;
    for($i = 1; $i <= $r; $i++)
        $p = $p * ($n-$i+1) / $i;
    return $p;
}

function paint() {
    //$n, $r, $t;

    for($n = 0; $n <= N; $n++) {
        for($r = 0; $r <= $n; $r++) {
            /* 排版设定开始 */
            if($r == 0) {  
                for($i = 0; $i <= (N-$n); $i++) {
                    echo "   ";
                }
            }
            else {
                echo "   ";
            } /* 排版设定结束 */
            echo combi($n, $r);
        }
        echo "<br />";
    }
}
paint();
?>


你可能感兴趣的:(PHP 帕斯卡(Pascal)三角形)