zoukankan      html  css  js  c++  java
  • 连续子数组的最大和

       1:  /*
       2:  @@
       3:      Author:    Justinzhang
       4:      Email:    uestczhangchao@gmail.com
       5:      time:    2012-9-1 21:40:13
       6:      desc:    find the max sum of sub arrys; This file was created at an earlier time; 
       7:              At that time, i didn't read the programming perls, in that book, there is a detailed
       8:              discussion about this problem. The fastest algrithm can solve it in O(n) time;
       9:              So if have spare time, read more of it.
      10:  @@
      11:  */
      12:   
      13:  #include <iostream>
      14:  #include <vector>
      15:  #include <algorithm>
      16:  #include <iomanip>
      17:  using namespace std;
      18:   
      19:  template<class type> 
      20:  type getMax(type param1, type param2)
      21:  {
      22:      return (param1>param2 ? param1 : param2);
      23:  }
      24:   
      25:  template<class type> type find_max_sum_of_subarray(vector<type> vec)
      26:  {
      27:      type maxSum = 0;
      28:      type curMaxSum = 0;
      29:      for(unsigned int i=0; i < vec.size(); i++)
      30:      {
      31:          curMaxSum += vec[i];
      32:          curMaxSum = getMax(0, curMaxSum);
      33:          maxSum = getMax(maxSum, curMaxSum);
      34:      }
      35:      return maxSum;
      36:  }
      37:   
      38:   
      39:  int main()
      40:  {
      41:      int array[] = {-2, 5, 3,-6, 44, -8, 16};
      42:      /* /!\ note that: to use array to initialize vector, the second parameter is the length of array,
      43:         not the length of array minus 1.
      44:      */
      45:      vector<int> vec(array,array+(sizeof array / sizeof(int)));
      46:      cout << "find_max_sum_of_subArray: " << find_max_sum_of_subarray<int>(vec) << endl;
      47:      return 0;
      48:  }
  • 相关阅读:
    Daemon Tools 4.x 或"SPTD.SYS"导致Windows不能启动的问题的解决方案
    Oracle for Windows 相关下载地址
    Winsock编程入门 4.面向连接的通讯
    MD5 Hashing in Java,Written by dimport
    登记照的尺寸
    使用FileUpload组件上传文件
    两台winXP电脑不能互相访问共享文件夹故障的最终解决方法
    常用序列号
    万事皆有因
    利用注册表检测IIS是否安装
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667178.html
Copyright © 2011-2022 走看看