要求输入n=5,输出n阶杨辉三角形(输出时没个数的宽度为5,左对齐)
时间: 2023-05-17 14:07:07 浏览: 14
1-5_Exe1_5_
我可以为您提供n阶杨辉三角形的代码:
```
n = 5
triangle = [[1]]
for i in range(1, n):
row = [1]
for j in range(1, i):
row.append(triangle[i-1][j-1] + triangle[i-1][j])
row.append(1)
triangle.append(row)
for row in triangle:
print(" ".join(str(num).ljust(5) for num in row))
```
输出结果为:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
阅读全文