CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day07/yanghui.py
Views: 729
1
"""
2
输出10行的杨辉三角 - 二项式的n次方展开系数
3
1
4
1 1
5
1 2 1
6
1 3 3 1
7
1 4 6 4 1
8
... ... ...
9
10
11
Version: 0.1
12
Author: 骆昊
13
Date: 2018-03-06
14
"""
15
16
17
def main():
18
num = int(input('Number of rows: '))
19
yh = [[]] * num
20
for row in range(len(yh)):
21
yh[row] = [None] * (row + 1)
22
for col in range(len(yh[row])):
23
if col == 0 or col == row:
24
yh[row][col] = 1
25
else:
26
yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1]
27
print(yh[row][col], end='\t')
28
print()
29
30
31
if __name__ == '__main__':
32
main()
33
34