当前位置:网站首页>杨辉三角(c语言实现)
杨辉三角(c语言实现)
2022-08-01 09:12:00 【学Java的冬瓜】
目录
一、什么是杨辉三角?
特征:图形两条斜边都是由数字1组成的,而其余的数则是等于它肩上的两个数之和。
如图:

二、实现方法
1、近似实现
注意当把它的全部元素左对齐,就可以看成近似杨辉三角的样子
如图:

1.1、法一 观察法
观察图片,找i==j行和第一列j==0时直接赋值为1
其他数等于肩上两数之和
//法一 观察初始化
int main()
{
int i = 0;
int j = 0;
int n = 0;
scanf("%d", &n);
int a[10][10] = { 0 };
//存放元素
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (j == 0 || i == j) //第一列和i==j时直接赋值为1
{
a[i][j] = 1;
}
if (i >= 2 && j >= 1)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
//打印
for (i = 0; i < n; i++)
{
for (j = 0; j <=i; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}1.2、法二 部分棋盘打印法
利用扫雷打印棋盘的方法,数组多创建行列多创建两行两列的方式,防止数组越界(也可以行+1,列+1,因为最后一行的下一行,列最后一行下一行(和正规杨辉三角不同)不会用到,没有越界问题)
//法二 采用扫雷初始化的方法,把数组空间行和列各创建大一行,防止越界
int main()
{
int a[10][10] = { 0 };
int i = 0;
int j = 0;
int n = 0;
a[0][0] = 1;
//输入数字
scanf("%d", &n);
for (i = 1; i <= n; i++) //实际使用的数组大小是a[n+1][n+1],多用到i=0行j=0列,但不打印出来
{
for (j = 1; j <= i; j++)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
//打印
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}2、标准杨辉三角
1、利用扫雷打印棋盘的方法,数组多创建两行两列(或者行+1,列+2,因为最后一行的下一行不会用到,而列的要用到)
2、数组空间可以自己改大再去看更大的杨辉三角
3、打印出来的空格处在数组中其实是已经初始化为0,打印时当满足数组元素==0,就打印空格
代码如下:
//法三 正规杨辉三角 (防止越界行多创建一行,列多创建两列)
//保存数据
void set(int a[10+1][2 * 10 + 1], int n) //[][]和a本身空间要一致,不然行列数会发生变化
{
int i = 0;
int j = 0;
a[1][n] = 1;
for (i = 1; i <=n ; i++)
{
for (j = 1; j <= 2 * n - 1; j++)
{
if (i == 1 && j == n)
continue;
else
a[i][j] = a[i - 1][j - 1] + a[i - 1][j + 1];
}
}
}
//初始化
void Init(int a[10+1][2 * 10 + 1], int n)
{
int i = 0;
int j = 0;
for (i = 0; i < n+1; i++)
{
for (j = 0; j < 2 * n + 1; j++)
{
a[i][j] = 0;
}
}
}
//打印
void print(int a[10+1][2 * 10 + 1], int n)
{
int i = 0;
int j = 0;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= 2 * n - 1; j++) //最右边一列不打印
{
if (a[i][j] == 0)
{
printf(" "); //3个空格,格式好看
}
else
{
printf("%3d", a[i][j]); //占3个字符,格式好看
}
}
printf("\n");
}
}
int main()
{
int n = 0; //a[i][j] = a[i - 1][j - 1] + a[i - 1][j + 1] - 2 * '0';
scanf("%d", &n); //char的数组算,当字符大于'9'后会出问题
int a[10+1][2*10+1] = { 0 }; //画图后发现:行=n+1,因为和最后一行的下一行无关
Init(a, n); // 列=2*n+1 ,多创建两行列
set(a, n);
print(a, n);
return 0;
}成果展示:

边栏推荐
- 扁平数组转树结构实现方式
- [Interview: Concurrency 39: Multithreading: Thread Pool] ThreadPoolExecutor Class - Submit, Stop
- How does UXDB return the number of records for all tables in the current database?
- Get the Token from the revised version of Qubutu Bed
- 2022杭电中超杯(1)个人题解
- Gethostbyname \ getaddrinfo DNS domain name IP address is not safe
- PerViT: 神经网络也能像人类利用外围视觉一样观察图像!
- 套接字选项
- Install GBase 8 c database, the error shows "Resource, how to solve?
- HoloView 在 jyputer lab/notebook 不显示总结
猜你喜欢

解析MySQL数据库:“SQL优化”与“索引优化”
![[Beyond programming] When the fig leaf is lifted, when people begin to accept everything](/img/e1/ff8d416c99e1f370d73b9520654ddf.jpg)
[Beyond programming] When the fig leaf is lifted, when people begin to accept everything

network basic learning

22牛客多校1 C.Grab the Seat (几何 + 暴力)

Redis 3.2.3 crashed by signal: 11 服务宕机问题排查

Data Analysis 6

Redis中间件(从搭建到弃坑)

【STM32】入门(一):环境搭建、编译、下载、运行

Chapter 9 of Huawei Deep Learning Course - Convolutional Neural Network and Case Practice

How to ensure the consistency of database and cache data?
随机推荐
静态Pod、Pod创建流程、容器资源限制
mysql查看cpu使用情况
network basic learning
Graduation thesis writing skills
Pod环境变量和initContainer
Lsky Pro 企业版手动升级、优化教程
pytest接口自动化测试框架 | parametrize中ids的用法
热修复技术可谓是百花齐放
leetcode-6132: Make all elements in array equal to zero
HoloView -- Tabular Datasets
The soul asks: How does MySQL solve phantom reads?
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
How to ensure the consistency of database and cache data?
力扣周赛304 6135. 图中的最长环 内向基环树
Gethostbyname \ getaddrinfo DNS domain name IP address is not safe
Microsoft Azure & NVIDIA IoT developers season I | Azure IoT & NVIDIA Jetson development foundation
UXDB如何返回当前数据库所有表的记录数?
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作
Flink SQL - client, how to deal with the source side and to increase the target, the SQL - client including mapping table and the JOB such as
Mysql数据库的部署以及初始化步骤