用队列实现“杨辉三角

本文探讨了一种不常用的方法,即利用队列来实现杨辉三角的生成,虽然相比多维数组更复杂,但提供了不同的实现思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果仅仅想打印出杨辉三角,用多维数组便可以轻松实现

使用队列反而复杂了,以下介绍用“复杂”的队列实现“简单”的杨辉三角

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 1000
typedef int datatype;

typedef struct node
{
	datatype data[MAX];
	int front, rear;
}queue;

void intitQueue(queue* &a) {
	a = (queue*)malloc(sizeof(queue));
	a->front = 0;
	a->rear = 0;
}

void EnQue(queue*& a, int x) {
	
	a->data[a->rear] = x;
	a->rear++;
}
int Getsum(queue*& a) {
	int x=a->data[a->front] + a->data[a->front + 1];
	a->front++;
	return x;
}

void PrintQueue(queue* a) {
	
	int m =a->front, n = a->rear;
	while (m< n) {
		if (a->data[m] != 0)
			printf("%d ", a->data[m]);
		m++;
	}
	printf("\n");
}


int main() {
	queue* m, * n;
	int x;
	printf("请输入要打印的行数:");
	scanf_s("%d", &x);
	intitQueue(m), intitQueue(n);
 
	m->data[0] = 0, m->data[1] = 1, m->data[2] = 0, m->rear =3;
	
	int j = 0;

	while (j < x) {
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值