方法一:
lst = [[1]]
n = 6
for i in range(1,n-1):
if i == 1:
lst.append([1,1])
newlst = [1]
for j in range(i):
newlst.append(lst[-1][j]+lst[-1][j+1])
newlst.append(1)
lst.append(newlst)
print(lst)
方法 二:
pre = [1]
print(pre)
n = 6
for i in range(1,n):
newlst = []
# pre.insert(0, 0)
pre.append(0)
for j in range(i+1):
newlst.append(pre[j-1]+pre[j])
print(newlst)
pre = newlst
方法 三:
triangle = []
n = 6
for i in range(n):
row = [1]*(i+1)
triangle.append(row)
for j in range(1,i//2+1):
val = triangle[i-1][j-1] + triangle[i-1][j]
row[j]=val
if i!=2*j:
row[-j-1] = val
print(triangle)
网友评论