javaScript——楊輝三角

楊輝三角是計算二項式乘方展開式的係數時必不可少的工具、是由數字排列而成的三角形數表。javascript

        資料:楊輝三角第n行的第1個數爲1,第二個數爲1×(n-1),第三個數爲1×(n-1)×(n-2)/2,第四個數爲1×(n-1)×(n-2)/2×(n-3)/3…依此類推。html

        楊輝三角另一個重要的特性就是每一行首尾兩個數字都是一、中間的數字等於上一行相鄰兩個數字的和、即排列組合中一般所運用的: java

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

        根據以上性質、能夠利用函數很輕鬆地將楊輝三角運算出來、函數接受一個參數、即但願獲得楊輝三角的行數、代碼以下:函數

  
  
           
  
  
  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)、對於這樣的特性、最有效的辦法就是遞歸:ui

  
  
           
  
  
  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.         } 

完整代碼:spa

  
  
           
  
  
  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>
相關文章
相關標籤/搜索