num=int(input()) tr=[[1]] for i in range(2,num+1): tr.append([1]*i) for j in range(1,i-1): tr[i-1][j]=tr[i-2][j]+tr[i-2][j-1] print(tr) width=len(str(tr[-1][len(tr[-1])//2]))+3 column=len(tr[-1])*width for sublist in tr: result=[] for contents in sublist: result.append('{0:^{1}}'.format(str(contents),width)) print('{0:^{1}}'.format(''.join(result),column))请补全 代码,生成杨辉三角行
时间: 2023-06-11 21:05:28 浏览: 98
数,并打印出美观的杨辉三角。
答案:
num = int(input())
tr = [[1]]
for i in range(2, num+1):
tr.append([1]*i)
for j in range(1, i-1):
tr[i-1][j] = tr[i-2][j] + tr[i-2][j-1]
width = len(str(tr[-1][len(tr[-1])//2]))
column = len(tr[-1]) * width
for sublist in tr:
result = []
for contents in sublist:
result.append('{0:^{1}}'.format(str(contents), width))
print('{0:^{1}}'.format(' '.join(result), column))
阅读全文