js杨辉三角
/*可以直接复制,f12粘贴运行*/
var n = prompt("请输入阶数:", 9); n = n - 0;
var t1 = new date();
var data = new array();
function print(str) {
if (typeof str == "number") {
var width = n%10 + 60; //等边三角
/* var width = str.tostring().length * 10; //金字塔 */
var sp = '<span style="padding:5px;display:inline-block;text-align:center;width:' + width + 'px;">' + str + '</span>';
document.write(sp);
}
else {
document.write(str);
}
}
print('<div style=text-align:center;">');
for (var i = 0; i < n; i++) {
data[i] = new array();
for (var j = 1; j < i + 2; j++) {
print(getnum(i, j));
}
print("<br/>");
}
print("</div>");
var t2 = new date();
print("<p style='text-align:center;'>耗时为(毫秒):" + (t2 - t1) + "</p>");
function getnum(x, y) {
if ((y == 1) || (y == x + 1)) {
data[x][y] = 1;
return 1;
}
data[x][y] = data[x - 1][y - 1] + data[x - 1][y];
return data[x - 1][y - 1] + data[x - 1][y];
}
上一篇: jquery animate() Alternate 语法
下一篇: 1、ajax、axios总结