python用队列输出杨辉三角

Posted by 昆山吴彦祖 on 2017.12.19
#队列实现杨辉三角输出
def sanjiao(n):
    i = 1
    list = []
    while i<n:

        if i==1:
            print(1)
            list.append(0)
            list.append(1)
            list.append(0)
        else:
            list.append(0)
            j = 0
            x = list.pop(0)
            while j<i:
                y = list.pop(0)
                print(x + y, end='')
                list.append(x + y)
                x = y
                j = j + 1

            list.append(0)
            print('')

        i=i+1

sanjiao(10)


输出

1
11
121
1331
14641
15101051
1615201561
172135352171
18285670562881


队列