Data structure after-school exercises (Exercise 1) 1007 Maximum Subsequence Sum (25 points)

Given a sequence of  K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

初始一个错误点的代码:
#include <the iostream>
 the using  namespace STD;
 int main () 
{ 
    int N; 
    CIN >> N;
     int A [N];
     int SUM = 0 , sumMax = - . 1 , the startPoint = 0 , tempPoint = 0 , = N-endPoint, . 1 ;
     // SUM sumMax and the maximum number of records tempPoint temporary pointer 
    for ( int I = 0 ; I <N; I ++ ) { 
        CIN >> A [I]; 
        SUM + = A [I];
         IF (SUM < 0) {
            SUM = 0 ; 
            tempPoint = I; 
        } the else  IF (SUM> sumMax) { 
            sumMax = SUM; 
            the startPoint = tempPoint + . 1 ; 
            endPoint, = I; 
        } 
    } 
    IF (sumMax < 0 ) sumMax = 0 ; // should be noted that if no one of 0, then 0 should be output, the first item, last item 
    COUT sumMax << << "  " << A [the startPoint] << "  " << [endPoint,] A;
     return  0  ;
}

Validated for a long time and found that the i-th wrong, we must pay attention, tempPoint not +1 startPoint, the reason: for the first time accounting for the number 0 to 1, and the sum conditions <0 count down, it will, for the first time less than 0 , startPoint will be reset after a number, this error remind ourselves that each program should be carried out guesses test cases.

#include <the iostream>
 the using  namespace STD;
 int main () 
{ 
    int N; 
    CIN >> N;
     int A [N];
     int SUM = 0 , sumMax = - . 1 , the startPoint = 0 , tempPoint = 0 , = N-endPoint, . 1 ;
     // SUM sumMax and the maximum number of records tempPoint temporary pointer 
    for ( int I = 0 ; I <N; I ++ ) { 
        CIN >> A [I]; 
        SUM + = A [I];
         IF (SUM < 0 ) {
            SUM = 0 ; 
            tempPoint = I + . 1 ; 
        } the else  IF (SUM> sumMax) { 
            sumMax = SUM; 
            the startPoint = tempPoint; 
            endPoint, = I; 
        } 
    } 
    IF (sumMax < 0 ) sumMax = 0 ; // should be noted that if no one of 0, then the output should be 0, the first term, last term 
    cout << sumMax << "  " << A [startPoint] << "  " << A [endPoint];
     return  0 ;
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11227246.html