算法:最大子数组own
2015-08-13 12:04
260 查看
转载标明出处:http://i.cnblogs.com/EditPosts.aspx?postid=4726782&update=1
暴力法:
View Code
只扫描了一遍,时间复杂度为O(n);
以上方法输出结果都一样:7 10 43
暴力法:
// Max_Value.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; int Max(int *a,int low,int high,int &sub_left,int &sub_right,int &sub_sum) { if (low==high) { if (a[low]>0) { sub_sum=a[low]; sub_left=low; sub_right=high; } else sub_sum=0; } int tempSum=0; for (int i=low;i<=high;i++) { tempSum+=a[i]; if (tempSum<=0) { tempSum=0; sub_left=i+1; } if (tempSum>sub_sum) { sub_sum=tempSum; sub_right=i; } } return 0; } int _tmain(int argc, _TCHAR* argv[]) { int sub_left=0; int sub_right=0; int sub_sum=0; int a[]={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7}; Max(a,0,15,sub_left,sub_right,sub_sum); cout<<sub_left<<" "<<sub_right<<" "<<sub_sum<<endl; system("pause"); return 0; }
View Code
只扫描了一遍,时间复杂度为O(n);
以上方法输出结果都一样:7 10 43
相关文章推荐
- 将Mysq数据导入solr索引库
- 全部选中和删除选中商品的实现,点击一下全选,点击两下取消全选
- 最小生成树之PRIM-O(n^2)
- poj解题报告——2425
- ViewHolder到底用什么修饰?static?final?static final?
- <Stealth>游戏中的动画与寻路控制
- 闪电特效算法
- Spark Graphx 构建graph和aggregateMessages聚合消息
- 工具:七牛云备份VPS服务器文件
- Clementine 12.0 的使用安装(数据挖掘)
- 让整个网站变成灰色的做法
- 黑马程序员——OC笔记之购票系统应用实践
- 乐点 Android SDK 积分墙开发者文档
- 360启用1700W美元收购的新域名360.com
- Fixing the Great Wall
- 用十进制计算30!(30的阶乘),将结果转化成3进制进行表示的话,该进制下的结果末尾会有多少个0?
- 记实现一个功能
- [即时通讯]openfire启动失败解决办法
- 人民币贬值不是大问题
- WSAAsyncSelect模型中,FD_WRITE事件什么时候触发?