2018.08.09

用两个队列完成杨辉三角的输出:

思路:

队列一:

010

 

队列二:

 

0110

 

队列一中的元素,两个相连的进行相加就可以得到第下一行中的需要打印的元素,例如:第一行键盘输入0 1 0;第二行我需要打印 1 1, 1 1就可以通过(0+1)和(1+0)运算获得。但是我如果需要通过运算得到第三行的数据,我需要在第二行的1 1 的首尾各加上一个0,变成 0 1 1 0,这样子我才能通过两个相连相加获得第三行的元素,就是 1 2 1;由此就可以总结出规律:

当我将当前一行的元素两两相连相加时,我就能获得下一行的数据,同时我要在这段数据的前后插入一个0。

下面是代码实现:

头文件:

#ifndef _DUILIE_H                     //头文件部分,和普通顺序队列没区别
#define _DUILIE_H

#define SUCCESS 10000
#define FAILURE 10001
#define TRUE    10002
#define FALSE   10003
#define SIZE    1000

struct queue
{
	int data[SIZE];
	int front;
	int rear;
};
typedef struct queue Queue;
int init(Queue *q);
int empty(Queue q);
int insert(Queue *q, int e);
int getfront(Queue q);
int outfront(Queue *q);
int length(Queue q);
#endif

主函数:

#include "duilie.h"                //一开始都要进行的操作:初始化,判断是否为空,然后插入
#include <stdio.h>

int main()
{
	int ret, i, d, j,g;
	Queue queue;
	Queue queue1;
	Queue queue2;
	
	ret = init(&queue1);
	ret = init(&queue);
	if(ret == SUCCESS)
	{
		printf("init success!\n");
	}
	else
	{
		printf("init failure!\n");
	}
	
	ret = empty(queue);
	if(ret == FALSE)
	{
		printf("queue is not empty!\n");
	}
	else
	{
		printf("queue is empty!\n");
	}
	for(i = 0; i < 3; i++)
	{	
		scanf("%d",&d);
		ret = insert(&queue,d);
		if(ret == SUCCESS)
		{
			printf("insert %d success!\n", d);
		}
		else
		{
			printf("insert %d failure!\n",d);
		}
	}

	ret =  bianli(queue);

	for(i = 0; i < 5; i++)                  //核心代码部分:
	{                                       //先确定输出几行杨辉三角
		insert(&queue1,0);                  //然后就是出队去,取队头元素,相加,插入
		for(j = 0; j < i + 2; j++)
		{
			g = outfront(&queue) +  getfront(queue);
			insert(&queue1,g);
			printf("%d ",g);		
		}
		printf("\n");
		insert(&queue1,0);
		queue2 = queue;                       //最后交换队列1 和队列2,然后清空队列2
		queue = queue1;                       //在我的代码中,队列1是queue,队列2是queue2
		queue1 = queue2;
		clear(&queue1);		
	}



	return 0;
}

子函数功能封装:

#include "duilie.h"
#include <stdio.h>
#include <stdlib.h>

int init(Queue *q)
{
	if(NULL == q)
	{
		return FAILURE;
	}
	q->rear = q->front = 0;
	return SUCCESS;
}
int empty(Queue q)
{
	return (q.front == q.front)?TRUE:FALSE;
}
int insert(Queue *q, int e)
{
	if(NULL == q)
	{
		return FAILURE;
	}
	if((q->rear + 1) % SIZE == q->front)
	{
		return FAILURE;
	}
	q->data[q->rear] = e;
	q->rear = (q->rear + 1) % SIZE;
	return SUCCESS;
}
int getfront(Queue q)
{
	if(q.front == q.rear)
	{
		return FAILURE;	
	}
	return q.data[q.front];

}
int outfront(Queue *q)
{
	int e;
	if(NULL == q || q->front == q->rear)
	{
		return FAILURE;
	}
	e = q->data[q->front];
	q->front = (q->front + 1) % SIZE;
	return e;

	
}
int length(Queue q)
{
	return (q.rear - q.front + SIZE) % SIZE;
}
int clear(Queue *q)
{
	if(q == NULL)
	{
		return FAILURE;
	}
	q->front = q->rear;
	return SUCCESS;
}
void bianli(Queue q)
{
	int i;
	for(i = q.front; i != q.rear; i++)
	{
		if(q.data[i] != 0)
		{
			printf("%d \n", q.data[i]);
		}
	}

}

 程序运行结果显示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来为您解答这个问题。 首先,我们需要将数据整理成R可处理的格式。这里,我将数据手动输入到R中,并将其保存为data.frame对象: ```R transportation <- data.frame( year = 2002:2021, railway = c(366.34, 375.14, 401.82, 419.16, 436.38, 447.02, 471.85, 487.8, 503.12, 518.25, 516.16, 522.77, 516.81, 532.08, 531.9, 527, 524.47, 511.09, 492, 476), highway = c(71.31, 67.32, 68.07, 67.88, 67.03, 66.3, 66.35, 63.34, 60.7, 51.92, 51, 49.2, 48.62, 46.52, 56.11, 54.45, 54.74, 54, 53, 45), water = c(20.27, 22.01, 29.42, 28.44, 27.44, 26.56, 27, 28.27, 29.03, 30.09, 30.35, 32.27, 31.09, 29.1, 34.06, 33.38, 33.5, 34.8, 37, 39), air = c(1482.14, 1510.68, 1773.71, 1751.13, 1724.75, 1716.97, 1669.62, 1616.08, 1598.11, 1573.69, 1547.57, 1508.83, 1464.21, 1497.47, 1502.85, 1484.65, 1479, 1470, 1442, 1432) ) ``` 接下来,我们可以使用ggplot2库绘制各种图形来更好地理解数据。 首先,我们可以使用ggplot2库绘制各种运输方式的时间序列折线图,代码如下: ```R library(ggplot2) ggplot(melt(transportation, id.vars = "year"), aes(x = year, y = value, color = variable)) + geom_line() + xlab("Year") + ylab("Passenger Distance") + ggtitle("Passenger Distance by Transportation Type") + theme(legend.position = "bottom") ``` 运行上面的代码,可以得到一个绘制了四种运输方式的时间序列折线图。 接着,我们可以使用ggplot2库绘制各种运输方式的密度图,代码如下: ```R ggplot(melt(transportation, id.vars = "year"), aes(x = value, fill = variable)) + geom_density(alpha = 0.5) + xlab("Passenger Distance") + ylab("Density") + ggtitle("Passenger Distance Density by Transportation Type") ``` 运行上面的代码,可以得到一个绘制了四种运输方式的密度图。 最后,我们可以使用ggplot2库绘制各种运输方式的箱线图,代码如下: ```R ggplot(melt(transportation, id.vars = "year"), aes(x = variable, y = value, fill = variable)) + geom_boxplot() + xlab("Transportation Type") + ylab("Passenger Distance") + ggtitle("Passenger Distance by Transportation Type") ``` 运行上面的代码,可以得到一个绘制了四种运输方式的箱线图。 以上是本次数据分析的R代码和图形,希望能够帮到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值