当前位置:网站首页>Basic exercise of test questions Yanghui triangle (two-dimensional array and shallow copy)

Basic exercise of test questions Yanghui triangle (two-dimensional array and shallow copy)

2022-06-13 02:07:00 Jerry-hao

The upper and left boundaries are added here , Easy to calculate

n = int(input())

b=[ [0]*(n+1) for i in range(n+1)]



b[1][1]=1

#print(" Before operation :",b)

for i in range(1,n):
    i=i+1
    for j in range(i):
        j=j+1
        b[i][j]=b[i-1][j-1] + b[i-1][j]
        #print(' The first ',i,' That's ok ',b)



for i in range(n):
    i = i+1
    for j in range(i):
        j=j+1
        print(b[i][j],end=' ')
    print()

The creation of two-dimensional array , Careful shallow copy :

How to create a two-dimensional array as well as python in [0 ]* n And [0 for _ in range(n)] The difference and connection _ Jiansheng Tudou's blog -CSDN Blog

To simplify the : Output directly after the operation

n = int(input())

b=[ [0]*(n+1) for i in range(n+1)]



b[1][1]=1

#print(" Before operation :",b)
print(b[1][1])

for i in range(1,n):
    i=i+1
    for j in range(i):
        j=j+1
        b[i][j]=b[i-1][j-1] + b[i-1][j]
        print(b[i][j],end=' ')
    print()

原网站

版权声明
本文为[Jerry-hao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280547094808.html

随机推荐