ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1007 Maximum Subsequence Sum (25 分)

2019-08-03 09:06:27  阅读:226  来源: 互联网

标签:25 last sum flag Subsequence 1007 end subsequence first


Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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;
}

 

 

标签:25,last,sum,flag,Subsequence,1007,end,subsequence,first
来源: https://www.cnblogs.com/TBhacker/p/11293709.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有