当前位置:网站首页>Implementing Yang Hui triangle with cyclic queue C language
Implementing Yang Hui triangle with cyclic queue C language
2022-07-05 12:20:00 【A finger leaf next to the giant】
Abstract :
In a data structure experiment class , The teacher proposed the topic of using queues to realize Yang Hui triangle , But the Yang Hui triangle we came into contact with at that time has always been implemented in the form of two-dimensional arrays , Similar to the queue of one-dimensional array to realize Yang Hui triangle , This is something I have never thought of , Until I saw the following article , The author gives the algorithm diagram of realizing Yang Hui triangle with queue , This algorithm makes me suddenly enlightened , I think this algorithm is in the implementation of Yang Hui triangle , Is a very good algorithm . But the author did not write the implementation steps in the article , I'll take c Language loop queue to implement this algorithm .
Algorithm reference source : https://zhuanlan.zhihu.com/p/89667145
1. Algorithm analysis :
Program analysis : Print Yanghui triangle , Use the circular queue to realize the transformation of each line of Yang Hui triangle .
Algorithm analysis : By operation , Use the circular queue to realize the transition from the previous row to this row .
Intermediate number operation :
Int Variable 1(outOne) Record team leader ( Team head ) data , Then the head of the team moves one digit to the right , And use variable two (outTwo) Record this location data , The sum of the two variables enters the end of the team ., At last, the tail pointer advances by one
Finally, make up at the end of the team 1;
use n Record the number of lines to print ,i Record the number of lines to print .
When i by 1 when , No intermediate operation , Just make up at the end of the team 1, Then print a single line
When i by 2 when , No intermediate operation , Just make up at the end of the team 1, Then print a single line .
When i by 3 when , The intermediate operation is performed once , Make up at the end of the team 1, Then print a single line .
When i by 5 when , The middle number operation is performed three times , Make up at the end of the team 1, Then print a single line .
When i by i when , Intermediate number operation execution j=i-2 Time , Make up at the end of the team 1, Then print a single line ( for(int j=1;j<i-1;j++){}; )
2. Detailed code :
// Use queue to realize Yang Hui triangle
//what is Yang hui triangle ? A triangular figure composed of regular numbers arranged in a triangle . The implementation instance has marble probability
//, Like the arrangement of Yang Hui's triangle numbers , The probability of marbles entering the middle hole is higher than that of the edge
// Application sequence queue implementation , Analyze the relationship and transformation rules between rows
#include <stdio.h>
#define MAXSIZE 13 /* Set the maximum number of print lines */
typedef struct {
int data[MAXSIZE];
int front; /* The head pointer */
int rear; /* Tail pointer , If the queue is not empty , Just want the queue to be the next location of the element */
}SqQueue;
/* Initialize a queue to store a value Q*/
int InitQueue(SqQueue *Q ) {
/* Circular queue */
Q->front=0;
Q->rear=0;
return 1;
}
int printOut(SqQueue *Q ,int n){
int outOne=0;
int outTwo=0;
// Print from the first line
for(int i=1;i<=n;i++){
// Carry out operations , Do not execute when the first line , The second line is not executed
// The third line is executed once , The fourth line executes twice , The fifth line is executed three times , From 1331 To 14641
for(int j=2;j<i;j++){
outOne=Q->data[Q->front];
Q->front=(Q->front+1)%MAXSIZE;
outTwo=Q->data[Q->front];
Q->data[Q->rear]=outOne+outTwo;
Q->rear=(Q->rear+1)%MAXSIZE;
}
// After execution , Join the team at the end 1
Q->data[Q->rear]=1;
Q->rear=(Q->rear+1)%MAXSIZE;
// Print space :
for(int u=1;u<=n-i;u++){
printf(" ");
}
// Traverse
int single=Q->front;
while(single!=Q->rear){
printf("\2 %4d",Q->data[single]);
single=(single+1)%MAXSIZE;
}
printf("\n");
}
return 1;
}
int main(){
SqQueue Q;
int n=0;
while(n<=0||n>=13){
/* Control the number of printed lines not to be too large , The display is not standard */
printf(" Please enter the number of lines to print :");
scanf("%d",&n);
}
InitQueue(&Q);
printOut(&Q,n);
return 1;
}
3. Print the results :
边栏推荐
- MySQL installation, Windows version
- Multi table operation - sub query
- The evolution of mobile cross platform technology
- Codeforces Round #804 (Div. 2)
- MySQL constraints
- How to clear floating?
- MVVM framework part I lifecycle
- How to recover the information server and how to recover the server data [easy to understand]
- MySQL view
- 强化学习-学习笔记3 | 策略学习
猜你喜欢
嵌入式软件架构设计-消息交互
Understand redis persistence mechanism in one article
What is digital existence? Digital transformation starts with digital existence
Redirection of redis cluster
Take you two minutes to quickly master the route and navigation of flutter
Why do you always fail in automated tests?
Matlab label2idx function (convert the label matrix into a cell array with linear index)
自动化测试生命周期
Interviewer: is acid fully guaranteed for redis transactions?
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
随机推荐
Redis's memory elimination mechanism, read this article is enough.
Intern position selection and simplified career development planning in Internet companies
codeforces每日5题(均1700)-第五天
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
Principle of redis cluster mode
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
MySQL index (1)
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
Instance + source code = see through 128 traps
Design of music box based on assembly language
Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
Master the new features of fluent 2.10
Handwriting blocking queue: condition + lock
手机 CPU 架构类型了解
ACID事务理论
Select drop-down box realizes three-level linkage of provinces and cities in China
MySQL splits strings for conditional queries
Understanding the architecture type of mobile CPU
Halcon 模板匹配实战代码(一)
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug