python应用-给出行数,输出相应的杨辉三角

  

def main():
    num = int(input('Number of rows: '))
    yh = [[]] * num
    for row in range(num):
        yh[row] = [None] * (row + 1)
        for col in range(row+1):
            if col == 0 or col == row:
                yh[row][col] = 1
            else:
                yh[row][col] = yh[row - 1][col] + yh[row - 1][col - 1]
            print(yh[row][col], end='\t')
        print()


if __name__ == '__main__':
    main()
结果:
Number of rows: 5
1   
1   1   
1   2   1   
1   3   3   1   
1   4   6   4   1

  

posted @ 2018-03-11 22:16  圆柱模板  阅读(503)  评论(0编辑  收藏  举报