✎
编程开发网
首页
C语言
C++
面试
Linux
函数
Windows
数据库
下载
搜索
当前位置:
首页
->
基础
->
c++编程基础
HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】(二)
2014-11-24 13:06:11
·
作者:
·
浏览:
5
标签:
HDU1003
Max
Sum
最大
序列
问题
算法
分析
实现
include
int main(){ int t, n, maxLeft, maxRight, maxSum, temp; int thisLeft, thisSum; scanf("%d", &t); for(int id = 1; id <= t; ++id){ scanf("%d", &n); scanf("%d", &maxSum); thisLeft = maxLeft = maxRight = 0; thisSum = maxSum; if(thisSum < 0){ thisSum = 0; thisLeft = 1; } for(int i = 1; i < n; ++i){ scanf("%d", &temp); thisSum += temp; if(thisSum > maxSum){ maxSum = thisSum; maxLeft = thisLeft; maxRight = i; } if(thisSum < 0){ thisLeft = i + 1; thisSum = 0; } } printf("Case %d:\n%d %d %d\n", id, maxSum, maxLeft + 1, maxRight + 1); if(id != t) printf("\n"); } return 0; }
时间开销如下
呼呼,四种算法总算都实现了。实现过程虽然很受挫,但是很充实的说,结果也很让人愉快,A wonderful day~
首页
上一页
1
2
下一页
尾页
2
/2/2