设为首页 加入收藏

TOP

算法学习之最大子序列问题(二)
2013-02-08 14:29:38 来源: 作者: 【 】 浏览:418
Tags:法学 习之 最大 序列 问题

 

  cout 《 "The biggest subarray is:" 《 endl;

  for( ; begin <= interval; begin++)

  {

  cout 《 a[begin] 《 "\t";

  }

  return max_sum;

  }

  //

  int maxSubSum3(const vector<int> & a)

  {

  int max_sum = 0;

  int this_sum = 0;

  unsigned int begin = 0;

  unsigned int count = 0;

  for(unsigned int j = 0; j < a.size(); j++)

  {

  this_sum += a[j];

  count++;

  if(this_sum >max_sum)

  {

  max_sum = this_sum;

  begin = (j+1) - count;

  }

  else if(this_sum < 0)

  {

  this_sum = 0;

  count = 0;

  }

  }

  cout 《 "The biggest subarray is:" 《 endl;

  for(unsigned int i = begin; i < begin + count; i++)

  {

  cout 《 a[i] 《 "\t";

  }

  return max_sum;

  }

  int main()

  {

  vector<int> array(N);

  int sub_sum = 0;

  //   maxSubSum1(array);

  cout 《 "Please input " 《 N 《" number to the array:" 《 endl;

  for(unsigned int i = 0; i < N; i++)

  {

  cin 》 array[i];

  }

  sub_sum = maxSubSum3(array);

  cout 《 "\nThe biggest sum of the subarray is:" 《 endl;

  cout 《 sub_sum 《 endl;

  return 0;

  }

  代码中给出了三种不同的方法,第一种的时间复杂度为O(n^3)。第二种为O(n^2)。第三种为O(n)。

      

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇TYVJ P2067(质因数分解) 下一篇VC++2012图形化皇后问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: