編程之美 - 子數組的最大乘積

問題描述:
給定一個長度爲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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章