首页 > 代码库 > js杨辉三角
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杨辉三角
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。