标签:
1 5 1 2 -1 3 -2
5
实例分析:a[0]~a[10] ={ 0, -2, -3, 1, 2, 3, -2, 4};
第一次判断a[1] < 0; max_sum = -10000000;
a[2] < 0; max_sum = -10000000;
a[3] > 0; 所以 a[4] += a[3] = 3; max_sum = 3;
a[4] > 0; 所以 a[5] += a[4] = 6; max_sum = 6;
a[5] > 0; 所以 a[6] += a[5] = 4; max_sum = 6;
a[6] > 0; 所以 a[7] += a[6] = 10;max_sum = 10;
#include<cstdio> #include<algorithm> using namespace std; int a[1000005]; int main() { int N; scanf("%d", &N); while(N--) { int i, n, max_sum = -100000000; scanf("%d", &n); for(i = 1; i <= n; i++) { scanf("%d", &a[i]); if(a[i-1] > 0) a[i] += a[i-1]; if(a[i] > max_sum) max_sum = a[i]; } printf("%d\n", max_sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/123tang/p/5859680.html