js杨辉三角
Posted 记录下自己走过前端的坑~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js杨辉三角相关的知识,希望对你有一定的参考价值。
function Tree() { this.lines = [ [1] ] } var pp = Tree.prototype pp.genNode = function(line, i) { var top = line - 1 var topLine = this.lines[top] || [0, 0, 0] var curLine = this.lines[line] if (!curLine) { curLine = this.lines[line] = [] } if (i in curLine) return curLine[i] = (topLine[i - 1] || 0) + (topLine[i] || 0) } pp.genLine = function(n) { for (var i = 0; i <= n; i++) { this.genNode(n, i) } } pp.print = function() { for (var i = 0; i < this.lines.length; i++) { var el = this.lines[i] console.log(el.join(‘, ‘)) } } var tree = new Tree for (var i = 0; i < 10; i++) { tree.genLine(i) } tree.print()
以上是关于js杨辉三角的主要内容,如果未能解决你的问题,请参考以下文章