求一个序列的最大子序列和

//求一个数列的最大子序列和

#include <iostream>

#include <vector>



int MaxSubsequenceSum(const std::vector<int>& ivec)

{

     int ThisSum = 0, MaxSum = 0;

     for(std::vector<int>::const_iterator it = ivec.begin();

          it != ivec.end(); ++it)

     {

          ThisSum += *it;

          if(ThisSum < 0)    

               ThisSum = 0;

          else if(ThisSum > MaxSum)

               MaxSum = ThisSum;

     }

     return MaxSum;

}

int main(int argc, char const *argv[])

{

     std::cout<<"Please input some number:"<<std::endl;

     int num;    

     std::vector<int> ivec;

     while(std::cin>>num)

          ivec.push_back(num);

     for(std::vector<int>::const_iterator it = ivec.begin();

          it != ivec.end(); ++it)

          std::cout<<*it<<" ";

     std::cout<<std::endl;

     std::cout<<"The MaxSubsequenceSum is:"<<MaxSubsequenceSum(ivec)<<std::endl;

     return 0;

}

  

你可能感兴趣的:(序列)