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

边栏推荐
- Data Analysis 5
- 【面试:并发篇39:多线程:线程池】ThreadPoolExecutor类-提交、停止
- zip打包目录所有文件(含隐藏文件/夹)
- GO error handling
- 灵魂发问:MySQL是如何解决幻读的?
- Redis中间件(从搭建到弃坑)
- 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
- pytest接口自动化测试框架 | 跳过测试类
- 179. 最大数
- pytest interface automation testing framework | single/multiple parameters
猜你喜欢

Leicester Weekly 304 6135. The longest ring in the picture Inward base ring tree

C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
![ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API](/img/b3/0167c22f14b97eb0206696495af7b5.png)
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API

Case practice --- Resnet classic convolutional neural network (Mindspore)

Leetcode - 6135: the longest part of the figure

【Untitled】

HoloView -- Tabular Datasets

The soul asks: How does MySQL solve phantom reads?

【杭电多校第四场 B题】最短路图+缩点dp

How to ensure the consistency of database and cache data?
随机推荐
sql server, FULL模式, dbcc shrinkfile(2,1) 不能收缩事务日志,还是原来的大小,是为什么?
数据分析6
解析MySQL数据库:“SQL优化”与“索引优化”
leetcode-6135:图中的最长环
pytest interface automation testing framework | single/multiple parameters
请问用flinksql写入数据到clickhouse需要引入什么依赖吗?
pytest interface automation testing framework | skip test classes
pytest接口自动化测试框架 | 单个/多个参数
[Interview: Concurrency 39: Multithreading: Thread Pool] ThreadPoolExecutor Class - Submit, Stop
Shell执行SQL发邮件
网络个各种协议
Pod environment variables and initContainer
Parsing MySQL Databases: "SQL Optimization" vs. "Index Optimization"
SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
Intensive reading of ACmix papers, and analysis of its model structure
22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
USB 协议 (二) 术语
Lsky Pro 企业版手动升级、优化教程
巧妙利用unbuffer实时写入
pytest interface automation testing framework | pass in parameter values in the form of function return values