最大连续子数组之和

#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;
}

最大连续子数组之和

 

上一篇:【一起刷LeetCode】整数反转


下一篇:iOS阶段学习第19天笔记(协议-Protocol)