js杨辉三角

来源:互联网 发布:b站是什么软件 编辑:程序博客网 时间:2024/06/06 00:21
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title> </title>
  </head>
  <body>
    <script type="text/javascript">
alert('杨辉三角');
      function Combination(m,n) {
        if (n==0) {
          return 1;  //每行第一个数为1 
        }else if(m==n) {
          return 1;  //最后一个数为1  

        }else {

       //其余都是相加而来  

          return Combination(m-1,n-1)+Combination(m-1,n);
        }
      }
      function Pascal(n){   //杨辉三角,N为行数 
for (var i = 0; i < n; i++) { //一共N行  
  for (var j = 0; j <= i; j++) {  //每行数字的个数即为行号、例如第1行1个数、第2行2个数
document.write(Combination(i,j)+" ");
    }
    document.write("<br>");
  }
}


  </script>

 <!--  直接传入希望得到的杨辉三角的行数   -->   

 <input value="杨辉三角" type="button" onclick="Pascal(10);" />
  </body>
</html>
1 0
原创粉丝点击