当前位置:   article > 正文

浙江大学数据结构MOOC-课后习题-第一讲-Maximum Subsequence Sum

浙江大学数据结构MOOC-课后习题-第一讲-Maximum Subsequence Sum

题目汇总
浙江大学数据结构MOOC-课后习题-拼题A-代码分享-2024

题目描述
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≤i≤j≤K. 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 (≤10000). 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 <iostream>
//检查是否 不含正数
bool checkNoPosSeq(int a[], int N)
{   
    bool flag = true;
    for (int i = 0; i < N; i++)
    {
        if (a[i] > 0) flag = false;
    }
    return flag;
}
//检测是否含0
bool checkZeroInSeq(int a[], int N)
{
    bool flag = false;
    for (int i = 0; i < N; i++)
    {
        if (a[i] == 0) flag = true;
    }
    return flag;
}
bool checkNegZeroSeq(int a[], int N)
{   
    if (checkNoPosSeq(a, N) == true && checkZeroInSeq(a, N) == true)
    {
        return true;
    }
    return false;
}

int getMaxSubSeqSum2(int a[], int N, int& l, int& r, int min)
{
    for (int i = 0; i < N; i++)
    {
        if (a[i] == 0)
        {
            l = r = i;
            return 0;
        }
    }
    return -1;
}
int getMaxSubSeqSum(int a[], int N, int& l, int& r, int min)
{
    int curSum, maxSum;
    int start = 0;
    bool flag = true;
    curSum = maxSum = 0;
    for (int i = 0; i < N; i++)
    {
        curSum += a[i];
        if (a[i] >= 0) flag = false;
        if (curSum > maxSum)
        {
            maxSum = curSum;
            r = i;
            l = start;
        }

        else if (curSum < 0)
        {
            if (i == (N - 1))
            {   
                if (maxSum == 0 && flag != false)
                {
                    l = 0;
                    r = N - 1;
                    return maxSum;
                }
                else return maxSum;
            }
            else start = i + 1;
            curSum = 0;
        }
    }
    if (maxSum == 0)
    {
        l = 0;
        r = N - 1;
    }
    return maxSum;
}

int main()
{
    int a[100000];
    int k;
    int m, n, min;
    m = n = 0;
    std::cin >> k;
    min = 0;
    int maxSum;
    for (int i = 0; i < k; i++)
    {
        std::cin >> a[i];
    }
    if (checkNegZeroSeq(a, k))
    {
        maxSum = getMaxSubSeqSum2(a, k, m, n, min);
    }
    else maxSum = getMaxSubSeqSum(a, k, m, n, min);
    std::cout << maxSum << ' ' << a[m] << ' ' << a[n] << std::endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/642219
推荐阅读
相关标签
  

闽ICP备14008679号