杨辉三角——队列的应用

杨辉三角形是形如:

1

1   1

1   2   1

1   3   3   1

1   4   6   4   1

的三角形,其实质是二项式(a+b)的n次方展开后各项的系数排成的三角形,它的特点是左右两边全是1,从第二行起,中间的每一个数是上一行里相邻两个数之和。

使用《队列》的思想来实现杨辉三角的流程:

1>首先,需要初始化一个队列,即对头=队尾=0;

2>将第一行的元素1入队,接着操作第二行(一二行不需要求和操作,直接将元素入队即可);

3>从第三行开始,现在的对头指向N-1行,先将每行的固定元素1入队,然后循环操作求和过程:

将队首元素出队,并保存它的值temp;

获取当前队首的元素x,并进行temp=temp+x,且将temp入队;

4>循环结束后,队首在N-1行的最后一个元素处,现将其出队,然后将每行最后的固定元素1入队;

5>循环3、4步就可以输出杨辉三角形了。

注意:杨辉三角的特点是第N行的中间值等于N-1行两值的和,队列采用的是单进单出。

//使用队列输出杨辉三角
#include "stdafx.h"
#include<stdio.h>

#define MAXSIZE 50
#define FALSE 0
#define TRUE 1
typedef int QueueElemType;

typedef struct
{
	QueueElemType element[MAXSIZE];
	int front;//队头
	int rear;//队尾
}SeqQueue;

void InitQueue(SeqQueue *Q)//初始化
{
	Q->front = Q->rear = 0;
}

int EnterQueue(SeqQueue *Q, QueueElemType x)//入队
{
	if ((Q->rear + 1) % MAXSIZE == Q->front)///队列已经满了
		return FALSE;
	Q->element[Q->rear] = x;
	Q->rear = (Q->rear + 1) % MAXSIZE;
	return TRUE;
}

int DelQueue(SeqQueue *Q, QueueElemType *x)//出对
{
	if (Q->front == Q->rear)
		return FALSE;
	*x = Q->element[Q->front];
	Q->front = (Q->front + 1) % MAXSIZE;

	return TRUE;
}
int GetHead(SeqQueue *Q, QueueElemType *x)//取对头元素
{
	if (Q->front == Q->rear)
		return FALSE;
	*x = Q->element[Q->front];
	return TRUE;

}
int IsEmpty(SeqQueue *Q)
{
	if (Q->rear == Q->front)
		return TRUE;
	else
		return FALSE;
}
//创建杨辉三角,N表示三角形的行数
void YangHuiTriangle(int N)
{
	int n, i, x, temp;
	SeqQueue Q;
	InitQueue(&Q);
	EnterQueue(&Q, 1);//第一行元素入队
	for (n = 2; n <= N; n++)
	{
		EnterQueue(&Q, 1);//入队
		for (i = 1; i <= n - 2; i++)
		{
			DelQueue(&Q, &temp);//出队的数赋给temp
			printf("%d ", temp);
			GetHead(&Q, &x);
			temp = temp + x;
			EnterQueue(&Q, temp);
		}
		DelQueue(&Q, &x);//出队
		printf("%d ", x);
		EnterQueue(&Q, 1);
		printf("\n");
	}
	while (!IsEmpty(&Q))
	{
		DelQueue(&Q, &x);
		printf("%d ", x);

	}
}
void main()
{
	int N;
	printf("please input the N:");
	scanf("%d", &N);
	YangHuiTriangle(N);
	printf("\n");
}

下面是网上找的思路,也可以参考一下:

使用队列解决这个问题有1个小的技巧:在两个1的两边增加两个0,通过0来标记这一层的结束。即:0  1  0 (杨辉三角的第一行,我们是从第一行开始的)

0  1  0

1   1

1  2  1

1   3   3   1

1   4   6   4   1

程序如下:

//遍历循环链表并打印
void printQueue(Queue *q)
{
for(int i = 0; i < Qlength(q);++i)
printf("%d ",q->data[(q->front+i+q->Qsize)%q->Qsize]);
printf("\n");
}

//输出杨辉三角的第n行的元素
void YangHuiTriangle(int n)
{
int level = n;
//printf("请输入");
Queue myQueue;
initQueue(&myQueue,level);
//初始化第一行:使用0作为一行结束的标记
enQueue(&myQueue,0);
enQueue(&myQueue,1);
enQueue(&myQueue,1);
enQueue(&myQueue,0);
int x,y;
for(int m = 1;m < level;++m)
{
do{
//将整个队列的前两个求和放入队尾,并删除队首元素
deQueue(&myQueue,&x);
getHead(&myQueue,&y);
enQueue(&myQueue,x+y);
}while(y!=0);
//填充队尾标记
enQueue(&myQueue,0);
}
printQueue(&myQueue);

}
 

这个做法很巧妙:先让元素出队,再获得后面的那个元素,然后再将二者相加以后入队,重复这个工作,直到遇见0为止。这个0既是上一层队列结束的0,又是这一层队列开始的0。

最后附赠一份C++实现杨辉三角的代码:

//杨辉三角的C++代码
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	int i,j,t;
	int a[11][11];
	//左右两边全是1
	for(i=1;i<11;i++)
	{
		a[i][1]=1;
		a[i][i]=1;
	}
	for(i=1;i<11;i++)
	{
		if(i>=3)
		{
			for(t=2;t<i;t++)
			{
				a[i][t]=a[i-1][t-1]+a[i-1][t];
			}
		}
	}
	for(i=1;i<11;i++)
	{
		for(j=1;j<=i;j++)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
时间: 2024-08-04 21:48:35

杨辉三角——队列的应用的相关文章

数据结构上机4队列-杨辉三角2

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 typedef int Status, QElemType; //队列结构定义 typedef struct { QElemType *base; int front; int rear; } SqQueue; //初始化队列 Status InitQueue(SqQueue *Q, int Qsize) {

数据结构上机4队列-杨辉三角1

#include <stdio.h> #include <malloc.h> #define OK 1 #define OVERFLOW -1 #define ERROR 0 typedef int Status, QElemType; typedef struct { QElemType *base; int front; int rear; } SqQueue; Status InitQueue(SqQueue *Q, int Qsize) { Q->base = (QE

使用队列求解杨辉三角的第K层的所有元素

Java代码   package queue; import java.util.concurrent.ConcurrentLinkedDeque; /** * Created by Lanxiaowei * Craated on 2016/12/12 9:03 * 求解杨辉三角的第K层的所有元素 * 使用队列求解 */ public class YHTriangleWithQueue { public static void main(String[] args) throws Interru

用队列打印杨辉三角

#include<iostream> #include<cstdio> #include<queue> #include<algorithm> using namespace std; queue<int> Q; int temp; void tringle(const int n) { Q.push(1); for(int i=2;i<=n;i++) { Q.push(1); for(int j=1;j<=i-2;j++) { te

数据结构总结系列(四)——循环队列之杨辉三角

今天我们来写一个循环队列的应用哦! 解决的是杨辉三角问题~~ 对于这样一个上下多层之间有密切联系的数据,如果只是用数组和循环来解决的话,显然会浪费大量的空间和时间, 所以我们用队列来解决这一问题: 之所以选用循环队列也是因为它对于空间的利用是非常有效的,方便我们的工作: 开始定义结构体: typedef struct //定义循环队列 { int data[MAXMIZE]; int Front; int Rear; }RollQueue; 这里的最大值(MAXMIZE)大家可以用宏定义来自己定

打印杨辉三角(STL版队列)

#include <iostream> #include <queue> using namespace std; void YangHuiTriangle(int n); int main() { cout<<"请输入杨辉三角的层数:"; int x; cin>>x; YangHuiTriangle(x); return 0; } void YangHuiTriangle(int n) { queue<int> q; q.p

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

杨辉三角

1 package com.llh.demo; 2 3 /** 4 * 杨辉三角 5 * 6 * @author llh 7 * 8 */ 9 public class Test { 10 /* 11 * 杨辉三角 12 */ 13 public static void main(String[] args) { 14 int[] a = new int[11]; 15 int num = 1; 16 // 17 for (int i = 1; i <= 10; i++) { 18 for (i

杨辉三角实例菱形实例

杨辉三角实例 public class Hui { public static void main (String [] args){ int [][] a =new int [10][10]; for(int i=0;i<a.length;i++){ for(int j=0;j<=i;j++){ if(j==0||i==j){ System.out.print(" "+(a[i][j]=1)); }else {a[i][j]=a[i-1][j-1]+a[i-1][j];