当前位置:网站首页>Yanghui triangle code implementation
Yanghui triangle code implementation
2022-06-12 21:06:00 【Bald and weak】
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 5 1
. . . . . . . . . .
law :
1. The first row and the main diagonal are 1
2. Current value = The previous row is preceded by the previous column + Same column as previous row
Two dimensional array implementation :
Thinking analysis :
1. The first row and the main diagonal are 1
arr[0][n]=0;arr[i][i]=0
2. Current value = The previous row is preceded by the previous column + Same column as previous row
arr[i][j]=arr[i-1][j-1]+arr[i-1][j]
// Yang Hui triangle two-dimensional array implementation
#include<stdio.h>
#define MAX 10
int main()
{
int arr[MAX][MAX];
for(int i=0;i<MAX;i++)
{
for(int j=0;j<=i;j++)
{
if(j==0||i==j)
arr[i][j]=1;
else
arr[i][j]=arr[i-1][j-1]+[i-1][j];
printf("%-4d",arr[i][j]);
}
printf("\n");
}
}One dimensional array implementation :
Thinking analysis :
Each row of data is directly updated in this array
For example, the third line 1 2 1
On the fourth line 1 3 3 1, It's on the third line 1 2 1 On the basis of
We try to find rules from the front to the back :
The third line 1 2 1
In the fourth row 1 1+2=3 ● 1
The fifth row 1
● Place should be violet 2+1 Got 3, But at this point I found , The second data in the fourth row updates the original data to 3,2 It is no longer possible to use , From this it is found that , Data cannot be updated from front to back
Find the rules from the back to the front :
benefits : The main diagonal is updated each time 1
The third line 1 2 1
In the fourth row 1 2 1 1
1 2+1=3 1+2= 3 1
The fifth row 1 3 3 1 1
1 3 +1 =4 3+3=6 3+1=4 1
Code implementation :
// One dimensional array to achieve Yang Hui triangle
#include<stdio.h>
#define MAX 10
int main()
{
for(int i=0;i<MAX;i++)
{
for(int j=i;j>=0;j--)// Traverse from back to front
{
if(j==0||j==i)
{
arr[j]=1;
}
else
{
arr[j]+=arr[j-1];
}
printf("%-4d",arr[j]);
}
}
}If there are any mistakes, please let me know in the comments section , thank you
边栏推荐
- Access control system based on RFID
- My way of programming
- Is it safe to open an account in flush? How to open an account online to buy stocks
- Solution of multi machine room dynamic loop status network touch screen monitoring
- At the beginning of SAP QM, assign sampling strategy for quantitative characteristics
- SAP QM preliminary - cannot assign a sampling policy to an inspection characteristic when maintaining an inspection plan by executing transaction code qp02?
- JS deep and shallow copy
- leetcode:207. 课程表
- leetcode:210. 課程錶 II
- lintcode:127 · 拓扑排序
猜你喜欢

Library cache lock brought by add trandata

Vs2017 environmental issues

Solution of multi machine room dynamic loop status network touch screen monitoring

Access control system based on RFID

机器学习资料汇总

#113 Path Sum II

leetcode:207. 课程表

leetcode:210. Schedule II

中小型机房动力环境综合监控解决方案

Typescript definition type: type 'timeout' cannot be assigned to type 'number';
随机推荐
The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
Summary of machine learning materials
#141 Linked List Cycle
Scatter in pytorch_ () function
居家办公期间如何提升沟通效率|社区征文
ASCII code comparison table
(11) Image frequency domain filtering with OpenCV
To understand Devops, you must read these ten books!
Design and practice of Hudi bucket index in byte skipping
Mxnet record IO details
At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?
#113 Path Sum II
二分查找
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
MySQL + PostgreSQL batch insert update insertorupdate
杨辉三角代码实现
Circularly insert one excel column and the sum of multiple columns
CUDA out of memory
Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer