求子序列和的最大值
程序员文章站
2022-05-27 09:47:11
...
#include <iostream>
#include <vector>
using namespace std;
int max_seq(const int * data, int size){
int tmp = 0;
int last = 0;
for(int i = 0; i < size; i ++){
if(tmp <= 0){
tmp = data[i];
}else{
tmp += data[i];
}
if(last < tmp )
last = tmp;
}
return last;
};
int main(){
int N;
vector<int> data;
cin >> N;
int tmp;
for(int i = 0; i < N; i++){
cin >> tmp;
data.push_back(tmp);
}
cout << max_seq(&data[0], N) << endl;
return 0;
}