最大连续子数组之和
作者:互联网
#include <iostream>
#include <limits> using namespace std; int main()
{
int a[100];
int n = 0;
while (cin.peek() != '\n') cin >> a[n++];
int maxsum = INT_MIN;
int currsum = 0;
for (int i = 0; i < n; i++)
{
if (currsum < 0)
currsum = a[i];
else
currsum += a[i];
if (currsum > maxsum)
maxsum = currsum;
}
cout << maxsum << endl;
return 0;
}
#include <limits> using namespace std; int main()
{
int a[100];
int n = 0;
while (cin.peek() != '\n') cin >> a[n++];
int maxsum = INT_MIN;
int currsum = 0;
for (int i = 0; i < n; i++)
{
if (currsum < 0)
currsum = a[i];
else
currsum += a[i];
if (currsum > maxsum)
maxsum = currsum;
}
cout << maxsum << endl;
return 0;
}
标签:最大,int,cin,currsum,++,连续,数组,include,maxsum 来源: https://www.cnblogs.com/mofeng123/p/11716576.html