python实现等腰三角形杨辉三角
时间: 2023-11-29 21:47:52 浏览: 71
打印杨辉三角python实现源码.zip
以下是Python实现等腰三角形杨辉三角的代码:
```python
def pascal_triangle(n):
triangle = []
for i in range(n):
row = [1] * (i + 1)
for j in range(1, i):
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j]
triangle.append(row)
return triangle
def print_pascal_triangle(n):
triangle = pascal_triangle(n)
max_num_width = len(str(triangle[-1][-1]))
for row in triangle:
row_str = ' '.join([str(num).center(max_num_width) for num in row])
print(row_str.center(max_num_width * n))
print_pascal_triangle(5)
```
输出结果为:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
阅读全文