标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1231
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23789 Accepted Submission(s): 10638
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 100005 #define oo 0x3f3f3f3f int a[N]; int main() { int n; while(scanf("%d", &n), n) { int Max=-oo, Lx, L, Ly, i, sum=0, flag = 0; for(i=1; i<=n; i++) { scanf("%d", &a[i]); if(a[i]>=0) flag = 1; } Max = sum = Lx = Ly = L = a[1]; ///首先要把它们初始化, 因为我下面用的东西都是跟上面的比较的, 但是如果我要从 0 开始有的东西就比较不了 for(i=2; i<=n; i++) { if(sum<=0) ///如果 sum 小于0 就是前几个连续数的和的小于0,当然要重新记录sum里的值,因为这是连续的嘛!!! { sum = a[i]; L = a[i]; } else sum += a[i]; ///否则的话直接让 sum 的值加上 a[i] 就可以了
if(sum>Max) ///如果 sum 大于最记录的最大值的话, 当然要改变它的值 { Max = sum; Lx = L; Ly = a[i]; } } if(!flag) printf("0 %d %d\n", a[1], a[n]); else printf("%d %d %d\n", Max, Lx, Ly); } return 0; }
标签:
原文地址:http://www.cnblogs.com/YY56/p/4855766.html