By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .
close

About Xml Xhtml Xslt Dhtml Css Javascript

rss_feed Subscribe RSS

前几天帮一个学弟写的,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 at 7 years ago
info About author

info License/Terms

More from Kontext

local_offer Javascript

visibility 7
comment 0
thumb_up 0
access_time 3 months ago

JSON is commonly used in modern applications for data storage and transfers. Pretty much all programming languages provide APIs to parse JSON. 

open_in_new View

comment Comments (0)
Please log in or register to comment. account_circle Log in person_add Register
No comments yet.