Given a sequence of n integers (possibly negative) a[1], a[2], a[3],..., a[n], find the sequence such as a[i]+a[i+1] +…+a[j] The maximum value of the sub-segment sum.
#include <iostream>
#include "vector"
using namespace std;
/**
* kkmd66 四刷
* @return
*/
int main() {
int n;
while (cin >> n && n != 0) {
//存储该序列
vector<int> vector(n);
for (int i = 0; i < n; ++i) {
cin >> vector[i];
}
//依次遍历序列,找加和最大子串
long long max = vector[0];
for (int i = 0; i < vector.size(); ++i) {
//每个序列起始开始加和
long long sum = 0;
for (int j = i; j < vector.size(); ++j) {
sum += vector[j];
if (sum > max)
max = sum;
}
}
cout << max << endl;
}
return 0;
}