zoukankan      html  css  js  c++  java
  • 最大子数组的和与积

    最大子数组的和

    double get_max_sum(double a[], int length)
    {
        double cursum = a[length - 1];
        double maxsum = a[length - 1];
        int curi = length - 2;
        while (curi >= 0)
        {
            cursum = max(a[curi], a[curi] + cursum);
            maxsum = max(cursum, maxsum);
            -- curi;
        }
        return maxsum;
    }

    最大子数组的积

    double get_max_pro(double a[], int length)
    {
        double minEnd = a[length - 1];
        double maxEnd = a[length - 1];
        double maxresult = a[length - 1];
        int curi = length - 2;
        while (curi >= 0)
        {
            double end1 = a[curi] * maxEnd;
            double end2 = a[curi] * minEnd;
            minEnd = min(a[curi], min(end1, end2));
            maxEnd = max(a[curi], max(end1, end2));
            -- curi;
        }
        return maxEnd;
    }

    测试

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double is[] = {-2, 5, 3, -6, 4, -8, 6};
        double ds[] = {-2, 5, 3, -6, 4, -8, 6};
        cout << get_max_sum(is, 7) << endl;
        cout << get_max_pro(ds, 7) << endl;
        return 0;
    }
    出自datakv
  • 相关阅读:
    4、2 核心组件
    promise
    Content-Type
    $routeProvider
    广告
    $apply() $digest()
    异常
    switch
    autoprefixer
    $resource
  • 原文地址:https://www.cnblogs.com/datakv/p/5632418.html
Copyright © 2011-2022 走看看