算法导论09--动态表

一、目的

1.熟悉算法设计的基本思想
2.掌握Dynamic table的方法

二、内容与设计思想

有一个公司想开发一个关于花卉的百科全书,用户只要输入花卉的名称,就能够输出花卉的详细信息。花卉包括:牡丹、芍药、茶花、菊花、梅花、兰花、月季、杜鹃花、郁金香、茉莉花、海棠、荷花、栀子花、莲花、百合、康乃馨、玫瑰、格桑花等1000种。这个公司想提升花卉检索和存储效率,打算采用动态表(dynamic table)来实现。由于花卉的数量可能会增加,也可能会减少,所实现的动态表需要有如下功能:

  1. 能够插入数据
  2. 能够删除数据
  3. 能够检索数据
  4. 能够按照参数扩展规模或者缩减规模

三、使用环境

推荐使用C/C++集成编译环境。

四、实验过程

1、分别画出各个实验结果的折线图(存储效率和查询效率)

#include<bits/stdc++.h>
using namespace std;
#define N 18

int main()
{
	int i,j,l,r;
	double t;
	
	double p[19]={0,0.112,0.094,0.094,0.075,0.075,0.075,0.057,0.057,0.057,0.057,0.038,0.038,0.038,0.038,0.038,0.019,0.019,0.019};
	double q[19]={0};
	double e[N+2][N+1]={0};
	double w[N+2][N+1]={0};
	int root[N+1][N+1]={0};
	
	for (i=1;i<=N+1;i++)
	{
		e[i][i-1]=q[i-1];
		w[i][i-1]=q[i-1];
	}
	
	for (l=1;l<=N;l++)
	{
		for (i=1;i<=N-l+1;i++)
		{
			j=i+l-1;
			e[i][j]=1000;
			w[i][j]=w[i][j-1]+p[j]+q[j];
			for (r=i;r<=j;r++)
			{
				t=e[i][r-1]+e[r+1][j]+w[i][j];
				if (t<e[i][j])
				{
					e[i][j]=t;
					root[i][j]=r;
				}
			}
		}
	}
	
	cout<<endl;
	for (i=1;i<=N+1;i++)
	{
		for (j=0;j<=N;j++)
		{
			cout<<e[i][j]<<" ";
		}
		cout<<endl;
	}
	
	cout<<endl;
	for (i=1;i<=N+1;i++)
	{
		for (j=0;j<=N;j++)
		{
			cout<<w[i][j]<<" ";
		}
		cout<<endl;
	}
	
	cout<<endl;
	for (i=1;i<=N;i++)
	{
		for (j=1;j<=N;j++)
		{
			cout<<root[i][j]<<" ";
		}
		cout<<endl;
	}
	
	return 0;
}
上一篇:【C语言I博客作业09】


下一篇:数据分析课程交流(第09次课):算法专题6——对数几率回归