Raymond Raymond

JavaScript版本的杨辉三角

event 2010-05-10 visibility 606 comment 0 insights toc
more_vert
insights Stats
toc Table of contents

这篇文章用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
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts