当前位置:网站首页>杨辉三角(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;
}成果展示:

边栏推荐
- Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础
- Shell执行SQL发邮件
- leetcode 42. Catch the rain
- USB 协议 (二) 术语
- Lsky Pro 企业版手动升级、优化教程
- pytest interface automation testing framework | pass in parameter values in the form of function return values
- Gethostbyname \ getaddrinfo DNS domain name IP address is not safe
- VoLTE基础学习系列 | 企业语音网简述
- 数据分析5
- 安装GBase 8c数据库的时候,报错显示“Resource,如何解决?
猜你喜欢

Chapters 6 and 7 of Huawei Deep Learning Course
改版去不图床 Token 的获取

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

【Unity3D】相机

SaaS安全认证综合指南

【手撕AHB-APB Bridge】~ AHB地址总线的低两位为什么不用来表示地址呢?

22 Grab the Seat 1 C.Grab the Seat (Geometry + Violence)

Analysis of High Availability Solution Based on MySql, Redis, Mq, ES

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

Idea 常用插件
随机推荐
Chapters 6 and 7 of Huawei Deep Learning Course
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes
various network protocols
最新的Cesium和Three的整合方法(附完整代码)
微服务:事务管理
Prime Ring Problem(素数环问题)
杰理AD14N/AD15N---串口中断问题
JVM 运行时数据区与JMM 内存模型详解
安装GBase 8c数据库的时候,报错显示“Resource,如何解决?
The socket option
优炫数据库支持Oracle哪几种时间及日期类型
Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础
Manual upgrade and optimization tutorial of Lsky Pro Enterprise Edition
Redis learning
pytest interface automation testing framework | parametrize source code analysis
leetcode 42. Catch the rain
毕业论文写作技巧
华为深度学习课程第九章——卷积神经网络以及案例实践
三子棋(C语言实现)
sql server, FULL mode, dbcc shrinkfile(2,1) can not shrink the transaction log, or the original size, why?