zoukankan      html  css  js  c++  java
  • 求一个整形数组里子序列和最大的算法

    int maxSubSum(int *a,int len)
    {
        int maxSum = a[0];
        int thisSum = 0;
        for (int i = 0;i < len; i++)
        {
            thisSum += a[i];
            if (thisSum > maxSum)
            {
                maxSum = thisSum;
            }
            else if (thisSum < 0)
            {
                thisSum =0;
            }
           
        }
        return maxSum;
    }

    int main()

    {

      int arr[] = {-2,-3,-4,-10,-9,-4,-2,-4,-6};
        cout<<"最大子序列和是:"<<maxSubSum(arr,9)<<endl;

    }

  • 相关阅读:
    图像相似度
    二维数组 问题 E: 计算鞍点
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    【转载】2015 Objective-C 三大新特性 | 干货
  • 原文地址:https://www.cnblogs.com/TianMG/p/2637996.html
Copyright © 2011-2022 走看看