zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    Java_static
    Java_字符串操作
    Java_==
    Java_字符串
    Java_Random
    Java_Scanner
    杨辉三角
    颜色分类
    字符串倒序
    jQuery的基本事件
  • 原文地址:https://www.cnblogs.com/alone2015/p/6526910.html
Copyright © 2011-2022 走看看