HDOJ1003 Max Sum(脑洞)
2015-09-25 23:10
423 查看
题目链接:
t组样例,每组样例开始是n,后n个数字组成一个数组。问你连续的哪些数之和最大。
每读入一个数字加到tmp上,代表当前数之和,若tmp比ans大则更新ans,同时更新起始和结束的点。若tmp为负则更新起点为j+1,
且tmp重置为0。保证了ans能取得最大。
AC代码:
t组样例,每组样例开始是n,后n个数字组成一个数组。问你连续的哪些数之和最大。
每读入一个数字加到tmp上,代表当前数之和,若tmp比ans大则更新ans,同时更新起始和结束的点。若tmp为负则更新起点为j+1,
且tmp重置为0。保证了ans能取得最大。
AC代码:
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int MAXN = 1e5 + 5; const int INF = 0x3f3f3f3f; int n, a[MAXN]; int main(int argc, char const *argv[]) { int t; scanf("%d", &t); for(int cas = 1; cas <= t; ++cas) { scanf("%d", &n); int ans = -INF, tmp = 0, x, st, ed; for(int i = 1, j = 1; j <= n; ++j) { scanf("%d", &x); tmp += x; if(tmp > ans) { ans = tmp; st = i; ed = j; } if(tmp < 0) { i = j + 1; tmp = 0; } } printf("Case %d:\n", cas); printf("%d %d %d\n", ans, st, ed); if(cas != t) printf("\n"); } return 0; }
相关文章推荐
- 信息安全系统设计基础第三周学习总结
- 面试技巧
- activity启动模式之singleInstance
- Scala深入浅出进阶经典 第49讲:Scala中Variance代码实战及其在Spark中的应用源码解析
- Javascript 矢量绘图 Sketch.js
- C语言的关键字
- DateTools,可能是最好用的iOS日期工具库
- avaScript高级程序设计之在 HTML 中使用 JavaScript 第2.2讲笔记
- Scala深入浅出进阶经典 第48讲:Scala类型约束代码实战及其在Spark中的应用源码解析
- Python | The Zen of Python
- 从零开始学习iOS开发-股票记帐本1.0(2)
- java研发面经:滴滴、美团、网易、58赶集等
- 使用aChartEngine绘制折线图如何设置背景色?
- USACO 1.5 Superprime Rib (DFS)
- Lucene 的使用
- 《C++primer(第五版)》学习之路-第十章:泛型算法
- nfs了解
- 全局变量与局部变量
- NetBeans中两个窗体之间如何传值
- 4 关于tableViewCell 显示不是空白行 使线宽和屏幕一样