1007 Maximum Subsequence Sum (25 分)

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


参考博文:https://blog.csdn.net/weixin_38097576/article/details/82715413

#include <stdio.h>

int main()
{
    int n, flag = 1, first_flag = 1, positive_flag = 0;
    int sum = 0, summax = 0;
    int st = 0, end = 0, st_max = 0, end_max = 0, first = 0, last = 0;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        scanf("%d", &last);
        if (last >= 0) {
            positive_flag = 1;   // 检查是否有正数,有正数则正数标记置1
        }
        if (first_flag) {
            first = last;        // 因为没用数组记录所有数据,这里就要用一个标记判断并记录第一个数据
            first_flag = 0;
        }
        if (flag) {
            st = last;           // 当sum < 0的时候要将下一项设为新的子列的开始项,
            flag = 0;            // 但是因为那时还没读取,所以就用一个标记记录这件要做的事,
        }                        // 在下一个循环完成。

        sum += last;
        end = last;
        if (summax < sum) {      // 当前子列和小于最大和时,更新最大和,最大子列首项和末项
            summax = sum;
            st_max = st;
            end_max = end;
        }
        if (sum < 0) {           // 同上面flag项解释,这里flag置1,sum清零
            sum = 0;
            flag = 1;
        }
    }
    if (positive_flag)
        printf("%d %d %d", summax, st_max, end_max);
    else
        printf("%d %d %d", summax, first, last);
    return 0;
}
 
 
 

猜你喜欢

转载自www.cnblogs.com/TBhacker/p/11293709.html