杨辉三角是计算二项式乘方展开式的系数时必不可少的工具、是由数字排列而成的三角形数表。

        资料:杨辉三角第n行的第1个数为1,第二个数为1×(n-1),第三个数为1×(n-1)×(n-2)/2,第四个数为1×(n-1)×(n-2)/2×(n-3)/3…依此类推。

        杨辉三角另外一个重要的特性就是每一行首尾两个数字都是1、中间的数字等于上一行相邻两个数字的和、即排列组合中通常所运用的: 

                              C(m,n) = C(m-1,n-1)+C(m-1,n)

        根据以上性质、可以利用函数很轻松地将杨辉三角运算出来、函数接受一个参数、即希望得到杨辉三角的行数、代码如下:

 
  
  1. function Pascal(n){   //杨辉三角,N为行数    
  2.     //         
  3. }  
 在这个函数中用两个for循环进行嵌套、外层循环数为行数、内层循环为每行内的每一项、代码如下:
 
  
  1. forvar i = 0 ; i < n ; i++ ){   //一共N行   
  2.                 for ( var j = 0 ; j <= i ; j++ ) {  //每行数字的个数即为行号、例如第1行1个数、第2行2个数   
  3.                        
  4.                 }   
  5.                 document.write("<br/>");   
  6.             }   

而在每行中每一个数字均为组合数C(m,n)、其中m为行号(从0算起)、n为在该行中的序号(从0算起)、即:

 
  
  1. document.write(Combination(i,j)+"&nbsp;&nbsp;");   //引号里面的内容是两个html空格(&nbsp;&nbsp;)字符 

其中Combination(i,j)为计算组合数的函数、这个函数采用组合数的特性C(m,n) = C(m-1,n-1)+C(m-1,n)、对于这样的特性、最有效的办法就是递归:

 
  
  1. function Combination(m,n){   
  2.             if(n == 0) return 1;  //每行第一个数为1   
  3.             else if(m == n) return 1; //最后一个数为1   
  4.             //其余都是相加而来    
  5.             else return Combination(m-1,n-1)+Combination(m-1,n);   
  6.         } 

完整代码:

 
  
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.    
  7. <%--   
  8.     杨辉三角   
  9.   createDate 2010-7-8   
  10.   author 古道西风   
  11.  --%>   
  12.    
  13. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  14. <html>   
  15.   <head>   
  16.     <base href="<%=basePath%>">   
  17.        
  18.     <title>杨辉三角</title>   
  19.        
  20.     <meta http-equiv="pragma" content="no-cache">   
  21.     <meta http-equiv="cache-control" content="no-cache">   
  22.     <meta http-equiv="expires" content="0">       
  23.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  24.     <meta http-equiv="description" content="This is my page">   
  25.     <script type="text/javascript">   
  26.         function Combination(m,n){   
  27.             if(n == 0) return 1;  //每行第一个数为1   
  28.             else if(m == n) return 1; //最后一个数为1   
  29.             //其余都是相加而来    
  30.             else return Combination(m-1,n-1)+Combination(m-1,n);   
  31.         }   
  32.         function Pascal(n){   //杨辉三角,N为行数    
  33.             for( var i = 0 ; i < n ; i++ ){   //一共N行   
  34.                 for ( var j = 0 ; j <= i ; j++ ) {  //每行数字的个数即为行号、例如第1行1个数、第2行2个数   
  35.                     document.write(Combination(i,j)+"  ");   
  36.                 }   
  37.                 document.write("<br/>");   
  38.             }   
  39.         }   
  40.     </script>   
  41.   </head>   
  42.   <body>   
  43.              <!--  直接传入希望得到的杨辉三角的行数   -->    
  44.     <input value="杨辉三角" type="button" onclick="Pascal(10);" />   
  45.   </body>   
  46. </html>