杨辉三角实现

Posted cranx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杨辉三角实现相关的知识,希望对你有一定的参考价值。

1   # -*- coding: utf-8 -*-

def triangles():
    line = [1]
    while True:
        yield line
        line = [x + y for x, y in zip([0] + line, line + [0])]

n = 0
for t in triangles():
    print(t)
    n = n + 1
    if n == 10:
        break

2

def triangles1(): # 杨辉三角形 L = [1] while True: yield L L = [1] + [L[n] + L[n-1] for n in range(1, len(L))] + [1]

def triangles2(): # 杨辉三角形 L = [1] while True: yield L L = [L[n - 1] + L[n] for n in range(len(L))]

def triangles3(): # 杨辉三角形 L = [1] while True: yield L for n in range(1, len(L)): L[n] = pre[n] + pre[n - 1] L.append(1) pre = L[:]

n = 0 for t in triangles1(): # 直接修改函数名即可运行 print(t) n = n + 1 if n == 10: break

3

def triangles():
i = 1
L = [1]
while True:
yield L
L = [0] + L + [0]
L = [L[s]+L[s+1] for s in range(i+1)]
i += 1
n = 0
for t in triangles():
print(t)
n += 1
if n == 10:
break

 

 

4

 

 


def triangles():
    l = []
    while True:
        last = 0
        lr = []
        for i in l:
            lr.append(last + i)
            last = i
        lr.append(1)
        l = lr
        yield lr

以上是关于杨辉三角实现的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个 CSS 片段可以画一个三角形? [复制]

Cg入门16:Fragment shader - 片段级光照

如何在 C++ 中为 GLSL 片段着色器实现 iGlobalTime?

绘制三角形的OpenGL程序给出了一个黄色的屏幕

CSS实现三角形

如何为每个 WebGL 三角形设置单独的颜色?