JavaScript版本的杨辉三角

visibility 560 event 2010-05-10 access_time 26 days ago language 中文
more_vert

这篇文章用JavaScript实现了杨辉三角,和其他C、Java等等版本的都差不多。

代码

<HTML>
   <HEAD>
      <TITLE>输出题设中给出的9*9的杨辉三角</TITLE>
   </HEAD>
   <BODY>
      <SCRIPT LANGUAGE="JAVASCRIPT">
         var level=10,i,j;
         var yh=new Array();
         for(i=0;i<level;i++)
         {
         yh[i]=new Array();
         }
         for(i=0;i<level;i++)
         {
         for(j=0;j<=i;j++)
         {
         if(i==j||j==0)
         {
            yh[i][j]=1;
         }
         else
         {
            yh[i][j]=yh[i-1][j]+yh[i-1][j-1];
         }
         document.write(yh[i][j]+"  ");
         if(i==j)document.write("<br>");
         }//end loop j
         }//end loop i
      </SCRIPT>
   </BODY>
</HTML>

运行效果:

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1
info Last modified by Raymond 26 days ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts