Given the sequence of K integers { N . 1 , N 2 , ..., N K }, "successive sub-column" is defined as { N I , N I + 1 , ..., N J }, where 1. "The biggest sub-columns and" is defined as all consecutive sub-column element and the greatest. For example, a given sequence {-2, 11 -4, 13, -5, -2}, {11 which successive sub-columns, -4, 13 and 20 have the greatest}. Now ask you to write a program to calculate the maximum sub-columns and given sequence of integers.
This question is designed to test a variety of different algorithms performance data in a variety of situations. Each set of test data the following features:
- Data 1: sample and equivalent to the basic test accuracy;
- Data 2: 102 random integer;
- Data 3: 103 random integer;
- Data 4: 104 random integer;
- Data 5: 105 random integer;
Input formats:
Input line 1 gives a positive integer K ( ≤); line 2 gives the K integers, separated by a space therebetween.
Output formats:
And output largest sub-columns in a row. If all integers in the sequence are all negative, 0 is output.
Sample input:
6
-2 11 -4 13 -5 -2
Sample output:
20
#include <iostream> using namespace std; int main() { int a,sum=0,max=0,tmp; cin>>a; for(int i=0;i<a;i++){ cin>>tmp; sum+=tmp; if(sum<0) sum=0; else if(sum>max) max=sum; } cout<<max; return 0; }