OJ刷题------找到最大子序和
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int len;
cin >> len;
//声明变长数组
vector<int> nums(len);
for (int i = 0; i < len; i++)
{
cin >> nums[i];
}
int sum = nums[0];
int m = nums[0];
for (int i = 1; i < len; i++)
{
if (m > 0)
m += nums[i];
else
m = nums[i];
if (sum < m)
sum = m;
}
cout << sum << endl;
return 0;
}
运行结果: