杨辉三角形 编程打印具有如下形式的杨辉三角形(它的特点是左右两边全是1,从第二行起,中间的每一个数是上一行里相邻两个数之和,其中输出数据的行数n从键盘输入,并日n<=10。 程序运行結果示例1: input n( n<=10): 1 1 1 1 1 1 2 1 3 3 46 1 1 程序运行结果示例2: input m (n=10): 1 1 1 1 1 1 1 1 7 1 3 4 5 3 6 10 1 4 1 10 5 6 15 20 15 1 6 1 輸入格式:"%d" 輸出格式: 输入提示信息:"input n (n<=10): \n" 输出数据格式:“%4d” 数据换行:“\n”
时间: 2024-02-24 15:56:03 浏览: 33
好的,这是一个经典的编程问题,下面是实现杨辉三角形的 Python 代码:
```python
n = int(input("input n (n<=10): \n"))
# 初始化第一行
row = [1]
for i in range(n):
# 输出当前行
print(" " * (n - i), end="")
for j in range(len(row)):
print("%4d" % row[j], end="")
print()
# 计算下一行
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
```
运行结果:
```
input n (n<=10):
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
```
相关问题
由c语言编程打印具有如下形式的杨辉三角形(它的特点是左右两边全是工,从第二行起,中间的每一个数是上一行里相 邻两个数之和》,其中输出数据的行数口从键盘输入,并且n=108
好的,以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int n, i, j, k;
printf("请输入行数:");
scanf("%d", &n);
int a[n][n]; // 二维数组存储杨辉三角形
// 初始化
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
a[i][j] = 0;
}
}
// 赋值
a[0][n/2] = 1; // 第一行只有中间一个数是1
for (i = 1; i < n; i++) {
for (j = 1; j < n-1; j++) {
a[i][j] = a[i-1][j-1] + a[i-1][j+1]; // 根据公式计算
}
}
// 输出
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a[i][j] != 0) { // 只输出不为0的数
printf("%d ", a[i][j]);
}
}
printf("\n");
}
return 0;
}
```
这个杨辉三角形是以中间列为基准,所以我把第一行的1放在了中间列。输出结果如下:
```
请输入行数:10
1
1 1 1
1 2 2 2 1
1 3 4 4 4 3 1
1 4 7 8 8 7 4 1
1 5 11 15 16 15 11 5 1
1 6 16 26 31 31 26 16 6 1
1 7 22 42 57 63 57 42 22 7 1
1 8 29 64 99 120 120 99 64 29 8 1
```
由c语言实现编程打印具有如下形式的杨辉三角形(它的特点是左右两边全是工,从第二行起,中间的每一个数是上一行里相 邻两个数之和》,其中输出数据的行数口从键盘输入,并且n<=10
好的,以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int n, i, j, k;
printf("请输入行数:");
scanf("%d", &n);
int a[n][n]; // 二维数组存储杨辉三角形
// 初始化
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
a[i][j] = 0;
}
}
// 赋值
a[0][0] = 1; // 第一行只有一个1
for (i = 1; i < n; i++) {
a[i][0] = 1; // 每行第一个数都是1
a[i][i] = 1; // 每行最后一个数都是1
for (j = 1; j < i; j++) {
a[i][j] = a[i-1][j-1] + a[i-1][j]; // 根据公式计算
}
}
// 输出
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a[i][j] != 0) { // 只输出不为0的数
printf("%d ", a[i][j]);
}
}
printf("\n");
}
return 0;
}
```
这个杨辉三角形是以左对齐的方式输出的,所以每行的第一个数都在最左边,最后一个数都在最右边。输出结果如下:
```
请输入行数:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)