求杨辉三角中的某个数,python实现
m=int(input('line: ').strip().lstrip('0'))
while True:
k=int(input('number: ').strip())
if k == m or k == 1:
print(1)
exit(10)
elif k > m:
print('wrong')
continue
else:
break
dim=list()
for b in range(m):
dew=[1]
dim.append(dew)
if b == 0:
continue
for p in range(1,b):
dew.append(dim[b-1][p-1]+dim[b-1][p])
dew.append(1)
print(dim)
print(dim[m-1][k-1])
pre=[1]
for b in range(1,m):
post=pre[:]
post.append(0)
pre=[]
for p in range(len(post)):
pre.append(post[p-1]+post[p])
print(pre)
print(pre[k-1])
pre=[1]
for b in range(1,m):
post=pre[:]
post.append(0)
pre.append(1)
for p in range(0,len(post)//2+1):
pre[p]=post[p-1]+post[p]
if p*2 != len(post)-1:
# pre[-p-1]=pre[p]
pre[len(post)-1-p]=pre[p]
print(pre[k-1])