关于作者

Discuss / Python / 杨辉三角

杨辉三角

问题来源

创世丿枫叶

#1 Created at ...

def tri(n): #n 代表行数 if n == 1: L = [1] yield L elif n == 2: L = [1] yield L L = [1, 1] yield L else: #大于两行的情况 L = [1] #第一行 yield L # L = [1, 1] #第二行 yield L

    i = len(L) #i为2
    while i < n: #如果n为3,则循环需要成立,现在len{L}为2,成立
        L = [1] +[L[a] + L[a + 1] for a in range(0,i - 1)] +[1]   #如果是第三行,中间要是1+1就停止,即L[0]+L[1]
        i = i + 1
        yield L

n = int(input('输入:')) for i in tri(n): print(i)


  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.