本帖最后由 jackz007 于 2022-8-3 14:24 编辑
当然是指循环变量 j 的变化范围是从 1 到 i 了(不包括 i)。def yang(n):
r = [[1 for j in range(i + 1)] for i in range(n)]
for i in range(2 , n):
for j in range(1 , i):
r[i][j] = r[i - 1][j - 1] + r[i - 1][j]
return r
n = int(input('输入 n : ') . strip())
d = yang(n)
for i in range(n):
s = ' ' * (n - i - 1) * 2 + '%3d' % d[i][0]
for j in range(1 , i + 1):
s += ' %3d' % d[i][j]
print(s)
运行实况:D:\[00.Exerciese.2022]\Python>python yang.py
输入 n : 7
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
D:\[00.Exerciese.2022]\Python>python yang.py
输入 n : 12
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
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
D:\[00.Exerciese.2022]\Python>
|