problem:
And evaluates the sequence of the maximum number of columns section
Input:
Enter the number of columns of the first row number of elements n
The second row of each of the input element
Output:
And maximum output sequence
Sample input:
6
-2 11 -4 13 -5 -2
Sample output:
20
Idea: the number of columns add up each number, if the element + sum is greater than zero, it can make the greatest final results
#include <stdio.h> #include <iostream> #include <math.h> #include <string.h> using namespace std; const double PI=acos(-1.0); const int inf=0x7fffffff; int a[105]; int dp[105]; int n,m,mx,sum; int main(){ int n; mx=-inf; cin>>n; for(int i=0 ; I <n-; I ++ ) { CIN >> A [I]; MX = max (MX, A [I]); } IF (MX < 0 ) << COUT MX; // if each one is less than 0 , the maximum value is the maximum that the else { sum = 0 ; for ( int I = 0 ; I <n-; I ++ ) { IF (sum + a [I] < 0 ) { // if an element such as a sum, and 0, the direct sum = 0, indicates that all elements not selected before, from the start, after the selected number of SUM = 0 ; } the else { SUM + = a [I]; //The number of columns add up each number, if the element + sum is greater than 0, can make the final result of the maximum } MX = max (MX, SUM); // every time the selected number, the maximum storage time, the cycle complete sequence is the maximum and the array } } COUT << MX; return 0 ; }
Yesterday is history, tomorrow is a mystery, but today is a gift, that is why it's called present !