Tags: Javascript

前几天帮一个学弟写的,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


About author
Disclaimer
The opinions and comments expressed herein are my own personal opinions and do not represent my employer's view in any way.
Comments
No comments.
Add comment
Title
Title is required.
Name
Name is required.
Email
Please input your personal email with valid format.
Comments
Please input comment content.
Captcha Refresh
Input captcha:

Subscription

Statistics

Locations of visitors to this page