2023年最大子段和问题实验报告.docx





《2023年最大子段和问题实验报告.docx》由会员分享,可在线阅读,更多相关《2023年最大子段和问题实验报告.docx(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验四最大子段和问题L实验目的(1)掌握动态规划的设计思想并能纯熟运用;(2 )理解这样一个观点:同样的问题可以用不同的方法解决,一个好的算法是反复努力和重新修正的结果;2 .实验规定(D分别用蛮力法、分治法和动态规划法设计最大子段和问题的算法;(2)比较不同算法的时间性能;(3)给出测试数据,写出程序文档;3 .实验设备和软件环境操作系统:Windows 7( 6 4x)开发工具:Visual Stud i o 2 023.实验环节以下实验数据都是以数组a = -2, 11, -4, 13, -5, - 2为例子;蛮力法蛮力法是一方面通过两个f。r循环去求出所有子段的值,然后通过if语句查找
2、出maxsum, 返回子序列的最大子段和;分治法(1)划分:按照平衡子问题的原则,将序列(al, a2,,an)划提成长度相同的两个子序列(al, a 2,.,an/2ri(an/2+ 1 ,.,an);(2)求解子问题:对与划分阶段的情况和可递归求解,情况需要分别计算s 1 =max V1/2 akX! ak乙k = i (i= i 0时,b j =bj -l+aj0(2)、当 bj-l0 W, b(j =aj 然后做递归操作求出最大子段和;5.实验结果蛮力法# i nc 1 u de #include usin g names p a ce st d ;i n t manlifa(int
3、a , i n t x)int i, j, sum=O, maxsum=O;* f or (i = 0 ; i x ; i+)( for (j = i + 1 ; j s u m)o 0(。sum = a i ;o o oo if ( s um maxsum)。 (3ma x s u m = s um :。)0)。re t urn max sum;)int m a in()o ini y, sum;i n t a = -20, 1 1 , -4, 13, -5, 2 );int c = s izeo f ( a ) / s izeof ( i n t );sum = manl if a (a,
4、 c);cout s um;c i n y :。r eturn 0;分治法# i nclu d e #inclu d e usi n g namespace s t d ;i n t MaxSum(int a, in t left, i n t r i ght)(。int s u m = 0 , m i d Sum = 0, 1 e ft Sum = 0, r igh t Sum = 0 ;。int c e nt e r, s 1 , s 2, left s ,r ights;。i f (left = right), sum = a lef t ;else(。cente r = (left +
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2023 最大 问题 实验 报告

限制150内