杨辉三角定义如下:
1
/ \
1 1
/ \ / \
1 2 1
/ \ / \ / \
1 3 3 1
/ \ / \ / \ / \
1 4 6 4 1
/ \ / \ / \ / \ / \
1 5 10 10 5 1
def triangles():
… L = [1]
… while True:
… yield L
… L = L + [0] #L.append(0)
… L = [(L[i-1]+L[i]) for i in range(len(L))]
n = 0
results = []
for t in triangles():
print(t)
results.append(t)
n = n + 1
if n == 10:
break
本文介绍了一种使用Python生成杨辉三角的方法,并提供了一个简单的实现示例。通过迭代的方式不断生成新的行并添加到已有的三角形中。
624

被折叠的 条评论
为什么被折叠?



