码迷,mamicode.com
首页 > Web开发 > 详细

js杨辉三角

时间:2017-03-09 18:57:46      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:logs   ret   []   type   join   print   nbsp   function   var   

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杨辉三角

标签:logs   ret   []   type   join   print   nbsp   function   var   

原文地址:http://www.cnblogs.com/alone2015/p/6526910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!