编程之美 - 子数组的最大乘积

问题描述: 给定一个长度为N的整数数组,只允许用乘法,不许用除法,求数组中任意N-1个数字的最大乘积

想法1: 计算数组中除去 a[i]以外后的其他数字的乘积,然后在其中找到最大值。 s[i] = s[i-1]*arr[i-1];      t[i] = t[i+1]*arr[i+1];

p[i] = s[i]*t[i];

代码示例中 : 
calc1()

想法2: 找出程序中正数,负数和0 的个数

1 )  如果有2个0,那么乘积一定是0 2) 如果有一个0,如果有奇数个负数,那么乘积是0 3) 如果有一个0,如果有偶数个负数,去掉0后计算乘积 4) 如果都是正数,那么去掉最小的正数 5) 如果都是正数,那么去掉最小的正数 6) 如果有奇数个负数,去掉最大的负数 7) 如果有偶数个负数,去掉最小的正数。

代码示例中 : 
calc2()


#include <iostream>

using namespace std;

int calc1(int arr[], int len)
{
    int *p=NULL, *s=NULL, *t=NULL;
    int i = 0, max = 0, index = 0;

    if (len == 0)
        return 0;

    p = new int[len]; s = new int[len]; t = new int[len];
    s[0] = 1;
    for (i = 1; i < len; i++)
    {
        s[i] = s[i-1]*arr[i-1];
        cout << s[i] << "  ";
    }
    cout << endl <<endl;

    t[len-1] = 1;
    for (i = len-2; i >= 0; i--)
    {
        t[i] = t[i+1]*arr[i+1];
        cout << t[i] << "  ";
    }
    cout << endl <<endl;

    for (i = 0; i < len-1; i++)
    {
        p[i] = s[i]*t[i];
        if (p[i] > max)
        {
            index = i;
            max = p[i];
        }
        cout << p[i] << "  ";
    }
    cout << endl <<endl;

    cout << "Max value="<< max << "  index="<< index << endl;
    cout << "\n------------------"<< endl <<endl;

    delete[] p; delete[] s; delete[] t;
    p = NULL; s = NULL; t = NULL;

    return max;
}

int calc2(int arr[], int len)
{
    int i = 0, max = 1, index = 0;
    int zeroCnt = 0, posCnt = 0, negCnt = 0;
    int maxNegIdx = 0, minPosIdx = 0;

    if (len == 0)
        return 0;

    for (i = 0; i < len; i++)
    {
        if (0 == arr[i])
        {
            zeroCnt++;
        }
        else if(arr[i] > 0)
        {
            posCnt++;
            if (arr[i] < arr[maxNegIdx])
                minPosIdx = i;
        }
        else
        {
            negCnt++;
            if ((arr[maxNegIdx] > 0) || (arr[i] > arr[maxNegIdx]))
                maxNegIdx = i;
        }
    }

    if (zeroCnt > 1)
    {
        max = 0;
    }
    else if (zeroCnt == 1)
    {
        if (negCnt%2 == 1)
            max = 0;
        else
            for (i = 0; i < len; i++)
                if (arr[i] != 0)
                    max *= arr[i];
                else
                    index = i;
    }
    else
    {
        if (negCnt%2 == 1)
            for (i = 0; i < len; i++)
                if (i != maxNegIdx)
                    max *= arr[i];
                else
                    index = i;
        else
            for (i = 0; i < len; i++)
                if (i != minPosIdx)
                    max *= arr[i];
                else
                    index = i;
    }

    cout << "Max value="<< max << "  index="<< index << endl;
    cout << "\n------------------"<< endl <<endl;

    return max;
}

int main()
{
    int test[] = {3, 4, 2, 5, 6, 7, 8};
    int len = sizeof(test)/sizeof(test[0]);

    int test1[] = {2, 0, 4, 5, 6, 7, 8};
    int len1 = sizeof(test)/sizeof(test[0]);

    int test2[] ={2, 0, 4, 0, 6, 7, 8};
    int len2 = sizeof(test)/sizeof(test[0]);

    int test3[] = {2, 3, 4, -5, 6, 7, 8};
    int len3 = sizeof(test)/sizeof(test[0]);

    int test4[] = {2, 3, -4, 5, -6, 7, 8};
    int len4 = sizeof(test)/sizeof(test[0]);

    calc1(test, len);
    calc1(test1, len1);
    calc1(test2, len2);
    calc1(test3, len3);
    calc1(test4, len4);

    cout << "\n\n============================================" << endl << endl;

    calc2(test, len);
    calc2(test1, len1);
    calc2(test2, len2);
    calc2(test3, len3);
    calc2(test4, len4);

    cin >> len;
    return 0;
}

点赞