问题:

给定一个序列a1,a2..........an;求子序列的和最大问题dp[i]表示以ai结尾的子序列和,max为最大子序列的和。

#include <iostream>
using namespace std;

#define MAXSIZE 100

int a[MAXSIZE];
int dp[MAXSIZE];
int max = 0;
int main()
{
    int n;
    cin >> n;
    memset(dp, 0, MAXSIZE);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
    {
        if (dp[i-1] + a[i] > 0)
        {
            dp[i] = dp[i - 1] + a[i];
        }
        else
        {
            dp[i] = 0;
        }

        if (max < dp[i])
            max = dp[i];
    }

    cout << max << endl;
    return 0;
}

相关文章:

  • 2021-10-14
  • 2021-12-06
  • 2021-04-12
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2021-10-18
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
相关资源
相似解决方案