“插花问题”的动态规划法算法

news/2024/7/11 1:31:14 标签: 算法, J#, F#, .net, Blog
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
// :============================“插花问题”的动态规划法算法============================
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
#define F100
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
#define V100
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_103_755_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_103_755_Closed_Image" alt="" />
/**/ /*
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />插花问题描述:
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />将f束鲜花插入v个花瓶中,使达到最徍的视觉效果,
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />问题相关约定及插花要求:
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />鲜花被编号为1--f,花瓶被编号为1--v,花瓶按从小到
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />大顺序排列,一只花瓶只能插一支花,鲜花i插入花瓶j中的
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />视觉效果效果值已知,编号小的鲜花所放入的花瓶编号也小
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />问题求解思路:
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />花瓶j(1.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />小的鲜花所放入的花瓶编号也小);
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />设数组p[i][j]表示鲜花i插入花瓶j的好看程度,数组
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />q[i][j]表示[1..i]束鲜花插入[1..j]个花瓶所能得到的最大
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />好看程度,初始化q[0][0]=0;q[0][j]=0(1.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />是问题的解.
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />特别地,j束鲜花插入到前面的j只花瓶中,所得到的好看
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />程度是q[j][j]=p[1][1]+p[2][2]+...+[j][j].现将插花过
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />程按花瓶排列顺序划分成不同阶段,则在第j阶段,第i束鲜花
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />若放入第j号花瓶,最大好看程度是q[i-1][j-1]+p[i][j];第i束鲜
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />花若放入前j-1个花瓶中的某一个,所得的好看程度是q[i][j-1],
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />那么在第j阶段,插入第i束鲜花所能得到的最大好看程度为:
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />q[i][j]=MAX(q[i-1][j-1]+p[i][j],q[i][j-1]),要使q[i][j]
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />最大,应使q[i-1][j-1]和q[i][j-1]也最大
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />
*/

.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
#define MAX(A,B)((A)>(B)?(A):(B)) // 求取两数的最大值宏定义
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
#define F100 // 鲜花数最大值常量定义
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
#define V100 // 花瓶数最大值常量定义
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
// “插花问题”的初始化函数
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
// intf,v:鲜花数量,花瓶个数
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
// intp[][v]:鲜花i插入花瓶j的好看程度
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
void Flower_Initialize( int * f, int * v, int p[][V])
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_990_1166_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_990_1166_Closed_Image" alt="" />
... {
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
inti,j;
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />printf(
"输入鲜花数量及花瓶个数:");
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />scanf(
"%d%d",f,v);
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />printf(
"顺序输入各鲜花插入各花瓶的好看程度: ");
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(i=1;if;i++)
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(j=1;jv;j++)
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />p[i][j]
=i*j;
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
//scanf("%d",&p[i][j]);
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />
}

.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
//“插花问题”的动态规划法解决函数
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
//intp[][v]:鲜花i插入花瓶j的好看程度
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
//intf,v:鲜花数量,花瓶个数
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
//int*way:鲜花插入花瓶的插入方法结果
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
intIkebana(intp[][V],intf,intv,int*way)
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_1314_1824_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_1314_1824_Closed_Image" alt="" />
...{
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
inti,j,q[F][V],newv;
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />q[
0][0]=0;//初始化[没有一束花插入花瓶时],好看程度自然为0
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
//设置v个花瓶分别被插入v束鲜花时各号花瓶对应的(初始)最大好看程度
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(j=1;jv;j++)
.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_1441_1525_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_1441_1525_Closed_Image" alt="" />
...{
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />q[
0][j]=0;
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
//设置第j束鲜花放入第j号花瓶中的最大好看程度
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
q[j][j]=q[j-1][j-1]+p[j][j];
.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />}

.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(j=1;jv;j++)
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(i=1;ij;i++)//计算在第j阶段,插入第i束鲜花所能得到的最大好看程度
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
q[i][j]=MAX(q[i-1][j-1]+p[i][j],q[i][j-1]);
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />newv
=v;
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(i=f;i>0;i--)
.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_1693_1804_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_1693_1804_Closed_Image" alt="" />
...{
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
while(q[i-1][newv-1]+p[i][newv]q[i][newv])
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />newv
--;
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
//确定鲜花i插在花瓶newv中,并准备考虑前一只花瓶
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
way[i]=newv--;
.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />}

.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
return(q[f][v]);
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}

.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
//测试“插花问题”的动态规划法函数
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
voidRun_Ikebana()
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_1864_2085_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_1864_2085_Closed_Image" alt="" />
...{
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
//循环计数器,鲜花数量,花瓶个数,鲜花i插入花瓶j的好看程度,鲜花插入花瓶的插入方法结果
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
inti,f,v,p[F][V],way[F];
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />Flower_Initialize(
&f,&v,p);
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />printf(
"最大好看程度点数为%d ",Ikebana(p,f,v,way));
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />printf(
"插有鲜花的花瓶是: ");
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
for(i=1;if;i++)
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />printf(
"%4d",way[i]);
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}

.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
//:============================“插花问题”的动态规划法算法============================
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
intmain(intargc,char*argv[])
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_2194_2269_Open_Image" alt="" />.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_2194_2269_Closed_Image" alt="" />
...{
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
//Run_SubString();
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
Run_Ikebana();
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />printf(
" 应用程序运行结束! ");
.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />
return0;
.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}

.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />
.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" />


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=935875



http://www.niftyadmin.cn/n/595994.html

相关文章

Spring2的Bean的作用域、装配类型、依赖检查

Spring2的Bean的作用域和装配类型----Spring2升级学习笔记一、作用域1、singleton在Spring IoC容器中仅存在一个Bean实例&#xff0c;Bean以单例的方式存在。2、prototype每次从容器调用Bean时&#xff0c;都返回一个新的实例&#xff0c;即每次调用getBean()时&#xff0c;相当…

手机百度浏览器怎么设置繁体字_手机便签可以自己设置提醒声音吗?怎么设置...

随着智能手机和移动互联网的发展&#xff0c;现在很多手机上都有系统自带的便签app。不过&#xff0c;小编推荐大家下载安装使用敬业签。因为这是一款专门为上班族提高工作效率而量身打造的高效便签软件。它支持备忘内容跨平台(Windows电脑﹑安卓手机﹑苹果iPhone手机﹑iPad﹑苹…

webapp用户身份认证方案 JSON WEB TOKEN 实现

webapp用户身份认证方案 JSON WEB TOKEN 实现Deme示例&#xff0c;Java版 本项目依赖于下面jar包&#xff1a; nimbus-jose-jwt-4.13.1.jar (一款开源的成熟的JSON WEB TOKEN 解决方法&#xff0c;本仓库的代码是对其的进一步封装)json-smart-2.0-RC2.jar和asm-1.0-RC1.jar (依…

在doc中生成柱状图_E007 如何完成doc~docx互相转换,Word批量转换Pdf

Hi&#xff0c;How are you doing?我是职场编码&#xff08;CodeVoc&#xff09;。在E000中&#xff0c;我们介绍了Node.js、Ruby、Electron等工具下载安装。这期&#xff0c;给你演示一下由Electron联合Ruby制作的小工具。知乎视频​www.zhihu.com借助Electron官方Demo&#…

python第三方库分类大全_python 第三方库安装脚本(示例代码)

代码如下&#xff1a;#BatchInstall.pyimport oslibs {‘numpy‘,‘matplotlib‘,‘pillow‘,‘sklearn‘,‘requests‘,‘jieba‘,‘beautifulsoup4‘,‘wheel‘,‘networkx‘,‘sympy‘,‘pyinstaller‘,‘django‘,‘flask‘,‘werobot‘,‘pyqt5‘,‘pandas‘,‘pyopengl‘…

POE技术简介

本文摘自笔者编著的《网管员必读——网络基础》&#xff08;第2版&#xff09;一书。 9.4.1 POE技术简介 以太网供电技术的出发点是让IP电话、WLAN接入点、网络摄像头等小型网络设备可以直接从以太网线获得电力&#xff0c;毋庸单独铺设电力线&#xff1b;以简化系统布线&…

JavaScript中的作用域

原文&#xff1a;http://www.digital-web.com/articles/scope_in_javascript/ 作用域&#xff08;scope&#xff09;是JavaScript语言的基石之一&#xff0c;在构建复杂程序时也可能是最令我头痛的东西。记不清多少次在函数之间传递控制后忘记 this关键字引用的究竟是哪个对象&…

字词句段篇章语言训练人教版上册r_小学语文字词句段篇章教学

1. 小学三年级语文陈永梅主编的字词句段篇章第13课 和时间赛跑 的内读小学的时候&#xff0c;我的外祖母去世了。外祖母生前最疼爱我。我无法排除自己的忧伤&#xff0c;每天在学校的操场上一圈一圈地跑着&#xff0c;跑得累倒在地上&#xff0c;扑在草坪上痛哭。那哀痛的日子持…