打印杨辉三角的前10行。
时间: 2023-08-27 11:05:31 浏览: 58
好的,以下是杨辉三角前10行的打印:
```
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,以下每行按照规律依次排列出数字,例如第三行的1 2 1,第四行的1 3 3 1,以此类推。
相关问题
用c语言实现 打印杨辉三角前10行
以下是用C语言实现打印杨辉三角前10行的代码:
```c
#include <stdio.h>
int main() {
int rows = 10; // 杨辉三角的行数
int coef = 1; // 杨辉三角中的系数
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
coef = 1;
} else {
coef = coef * (i - j + 1) / j;
}
printf("%d ", coef);
}
printf("\n");
}
return 0;
}
```
输出结果为:
```
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
```
编程:打印杨辉三角前10行。
好的,以下是打印杨辉三角前10行的Python代码:
```
def yanghui_triangle(n):
if n == 1:
return [1]
else:
last_row = yanghui_triangle(n-1)
row = [1]
for i in range(n-2):
row.append(last_row[i] + last_row[i+1])
row.append(1)
return row
for n in range(1, 11):
print(" ".join(str(x) for x in yanghui_triangle(n)))
```
输出结果如下:
```
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
```