输出杨辉三角,用python
时间: 2024-01-26 09:41:58 浏览: 57
用Python输出一个杨辉三角的例子
好的,以下是用Python输出杨辉三角的代码:
```python
def printPascal(n):
arr = [[0 for x in range(n)] for y in range(n)]
for line in range(0, n):
for i in range(0, line + 1):
if i == 0 or i == line:
arr[line][i] = 1
print(arr[line][i], end=" ")
else:
arr[line][i] = arr[line - 1][i - 1] + arr[line - 1][i]
print(arr[line][i], end=" ")
print("\n")
n = int(input("请输入要输出的行数:"))
printPascal(n)
```
输出结果:
```
请输入要输出的行数:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
阅读全文