Advertisement

Python生成杨辉三角形( RUNOOB python练习题61)

阅读量:

作为编程学习的实践题目,原题来源: python练习实例61

题目描述:输出杨辉三角形的结构
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

对应的实现代码如下:

复制代码
    import numpy as np
    
    table = np.zeros((10,10),dtype=int)
    table[:,0] = 1
    table[1,1] = 1
    print(table)
    for i in range(2,table.shape[0]):
      for j in range(1,table.shape[1]):
    table[i,j] = table[i-1,j-1] + table[i-1,j]
    print(table)
    for i in range(table.shape[0]):
      for j in

全部评论 (0)

还没有任何评论哟~