JavaScript版本的杨辉三角

access_time 11 years ago visibility408 comment 0

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

Want to publish your article on Kontext?

Learn more

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.


Learn more arrow_forward

More from Kontext

local_offer node.js local_offer Javascript

visibility 2393
thumb_up 1
access_time 9 months ago

After upgrading Visual Studio to 16.5.1, I encountered the following error in Task Runner Explorer: ***\Kontext.Web.Portals\node_modules\node-sass\lib\binding.js:15 throw new Error(errors.missingBinary()); ^ Error: Missing binding ...

Bootstrap 4 Mega Full Width Dropdown Menu

local_offer HTML local_offer Javascript local_offer bootstrap

visibility 2246
thumb_up 2
access_time 7 months ago

On Kontext, a full width mega dropdown menu is implemented using Bootstrap 4. If you'd like to implement similar menus, please follow these steps.  The following code snippets work with Bootstrap 4.4.1. It should also work with other 4.x versions but I didn't test it.  The new CSS class ...

local_offer Javascript

visibility 21
thumb_up 0
access_time 2 years ago

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

About column

About Xml Xhtml Xslt Dhtml Css Javascript

rss_feed Subscribe RSS