好久没看书了,随便拿来《编程珠玑》翻到第8章,介绍了一个叫求数组中最大子数组的算法,是线性的时间复杂度。

  问题描述是具有n个浮点数的向量x,求向量中任何连续子向量的最大和。

#include <iostream>
using namespace std;

int main()
{
    int x[10]={31,-41,59,26,-53,58,97,-93,-23,84};
    int maxsofar=0;
    int maxendinghere=0;
    for (int i=0;i<10;i++)
    {
        maxendinghere=max(maxendinghere+x[i],0);
        maxsofar=max(maxsofar,maxendinghere);
    }
    cout<<maxsofar<<endl;
    cin.get();
    return 0;

}

相关文章:

  • 2021-09-18
  • 2022-01-21
  • 2021-11-20
  • 2022-03-05
  • 2021-12-09
  • 2021-07-30
  • 2022-03-07
  • 2021-10-29
猜你喜欢
  • 2021-09-03
  • 2021-09-19
  • 2022-01-25
  • 2022-01-16
  • 2021-11-04
  • 2021-11-01
  • 2022-01-03
相关资源
相似解决方案