数据结构与算法分析_数组的子序列问题
来源:互联网 发布:homebrew安装mysql 编辑:程序博客网 时间:2024/05/21 06:37
<span style="font-family:Arial;">/***2.17*求最小子序列和,最小正子序列和,最大子序列乘积**/#include <iostream>#include <algorithm> using namespace std;#define INT_MIN -32768#define INT_MAX 32767//在里面有使用#define和const定义常量的比较,推荐使用const//#define LEN 10const int LEN = 10;/***最大子序列和**/int subMaxSum(int *a, int length){int thismax = 0, max=INT_MIN;for(int i=0; i<length; i++){thismax += a[i];if(thismax > max){max = thismax;}else if(thismax < 0){thismax = 0;}}return max;}/***最小子序列和*n*/int subMinSum(int a[], int length){int thismin = 0, min = INT_MAX;for(int i=0; i<length; i++){thismin += a[i];if(thismin < min){min = thismin;}else if(thismin > 0){thismin = 0;}}return min;}struct Node { int sum; int xiabiao; }; int cmp(const Node& t1,const Node& t2) { return t1.sum < t2.sum;} /***最小正子序列和*n*logn*/int positiveSubMinSum(int *data, int len){Node* temp = new Node[len]; temp[0].sum = data[0]; temp[0].xiabiao = 0; for(int i=1;i<len;i++) { temp[i].sum = temp[i-1].sum+data[i]; temp[i].xiabiao = i; } //对temp.sum[]进行从小到大排序,sum[]中只有相邻的两个数才有可能 得到 最小正子序列和 sort(temp,temp+len,cmp); int sum = INT_MAX; for(int i=0;i<len-1;i++) { if(temp[i].xiabiao < temp[i+1].xiabiao) { if(temp[i+1].sum - temp[i].sum > 0 && temp[i+1].sum - temp[i].sum < sum) sum = temp[i+1].sum - temp[i].sum; } } delete temp; temp=0; return sum; }void swap(int& a, int& b){ int temp = a; a = b; b = temp;}/***最大子序列乘积(同时也求出了最小的子序列乘积)*在找最大的值得时候,必须记录最小值,因为有负数存在,最小的数可能变成最大的数*/int mutiSubMax(int *a, int length){ int i; int maxProduct = 1; int minProduct = 1; int maxCurrent = 1; int minCurrent = 1; for( i=0; i<length ;i++) { maxCurrent *= a[i]; minCurrent *= a[i]; if(maxCurrent > maxProduct) maxProduct = maxCurrent; if(minCurrent > maxProduct) maxProduct = minCurrent; if(maxCurrent < minProduct) minProduct = maxCurrent; if(minCurrent < minProduct) minProduct = minCurrent; //注意交换 if(minCurrent > maxCurrent) swap(maxCurrent,minCurrent); //这个必须在最后(防止为0的时候) if(maxCurrent<1) maxCurrent = 1; //if(minCurrent>1)//这里不需要,因为通过交换即可,只需要一个 // minCurrent =1; } return maxProduct;}int main(){int a[LEN] = {4,-1,5,-2,-1,2,6,-2,1,-3};cout<<"列表:"<<endl;for(int i=0; i<10; i++){cout<<a[i]<<" ";}cout<<endl;cout<<"最大子序列和:"<<subMaxSum(a, LEN)<<endl;cout<<"最小子序列和:"<<subMinSum(a, LEN)<<endl;cout<<"最小正子序列和:"<<positiveSubMinSum(a, LEN)<<endl;cout<<"最大子序列乘积:"<<mutiSubMax(a, LEN)<<endl;return 0;}</span>
0 0
- 数据结构与算法分析_数组的子序列问题
- 数据结构与算法分析--最大子序列和问题
- 《数据结构与算法分析》笔记------第二章、最大子序列和问题的解
- 数据结构与算法分析——最大的子序列和问题
- 【数据结构与算法】最大子序列和问题的求解
- [数据结构与算法分析] 求连续子数组的最大和问题
- <数据结构与算法分析 C++描述>算法分析之最大子序列和问题
- 数据结构与算法分析 学习笔记——最大子序列求和问题
- [数据结构与算法分析]最大子序列和问题 -学习笔记
- 数据结构与算法分析之最大子序列和
- 【数据结构与算法分析】2.4 求最大子序列和
- 《算法设计与分析》学习笔记_第六章_最长单调递增子序列个数问题
- 【算法与数据结构】最大子序列和问题
- 第7周 数据结构与算法分析 2-8求最大子序列的函数
- 数据结构与算法总结2_常用的数据结构(数组)
- 求最大子序列和问题(读《数据结构与算法分析——C语言描述》有感)
- 数据结构与算法分析笔记与总结(java实现)--数组5:连续子数组的最大和
- 数据结构与算法 求最大子序列
- 170.Rotate List-旋转链表(中等题)
- 多文件中共享变量
- Unity 使用Profile查看真机性能
- ArrayList
- mysql 字段类型
- 数据结构与算法分析_数组的子序列问题
- 《 JavaScript&jQuery交互式Web前端开发》读书笔记二:各种页面加载事件的区别
- 网页设计中常用字体
- MAC ReactNative环境搭建
- crf++学习资料
- 图像增强——伽马变换
- 微信公众平台-微信发送朋友、分享到QQ、分享QQ空间、分享腾讯微博-JSSDk接口
- C++正则表达式使用
- 关于图像膨胀和腐蚀的介绍