、输出以下杨辉三角形(要求输出10行)。(
时间: 2024-05-07 12:22:34 浏览: 95
1-5_Exe1_5_
以下是输出10行杨辉三角形的 Python 代码:
```python
n = 10
# 初始化杨辉三角形第一行
row = [1]
# 遍历杨辉三角形的每一行
for i in range(n):
# 打印当前行
print(row)
# 计算下一行
next_row = [1]
for j in range(len(row)-1):
next_row.append(row[j]+row[j+1])
next_row.append(1)
row = next_row
```
输出结果为:
```
[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]
```
阅读全文