zoukankan      html  css  js  c++  java
  • 《剑指offer》第六十六题:构建乘积数组

    // 面试题66:构建乘积数组
    // 题目:给定一个数组A[0, 1, …, n-1],请构建一个数组B[0, 1, …, n-1],其
    // 中B中的元素B[i] =A[0]×A[1]×… ×A[i-1]×A[i+1]×…×A[n-1]。不能使用除法。
    
    #include <cstdio>
    #include <vector>
    
    using namespace std;
    
    void BuildProductionArray(const vector<double>& input, vector<double>& output)
    {
        //B[i] = ( A[0] * A[1] * ... * A[i-1] ) * ( A[i+1] * ... * A[n-1] )
        //     = C[i] * D[i]
        int length1 = input.size();
        int length2 = output.size();
    
        if (length1 == length2 && length1 > 1)
        {
            //由上到下计算左下三角矩阵的累计值
            //C[i] = A[0] * A[1] * ... * A[i - 1]
            //     = C[i - 1] * A[i - 1]
            output[0] = 1;
            for (int i = 1; i < length1; ++i)
            {
                output[i] = output[i - 1] * input[i - 1];
            }
    
            //由下到上计算右上三角矩阵的累计值
            //D[i] = A[i + 1] * A[i + 2] * ... * A[n - 1]
            //     = D[i + 1] * A[i + 1]
            int temp = 1; //保存D[i]值的中间变量
            for (int i = length1 - 2; i >= 0; --i)
            {
                temp = temp * input[i + 1];
                output[i] = output[i] * temp;
            }
        }
    }
    //================= Test Code =================
    static bool EqualArrays(const vector<double>& input, const vector<double>& output)
    {
        int length1 = input.size();
        int length2 = output.size();
    
        if (length1 != length2)
            return false;
    
        for (int i = 0; i < length1; ++i)
        {
            if (abs(input[i] - output[i]) > 0.0000001)
                return false;
        }
    
        return true;
    }
    
    static void test(const char* testName, const vector<double>& input, vector<double>& output, const vector<double>& expected)
    {
        printf("%s Begins: ", testName);
    
        BuildProductionArray(input, output);
        if (EqualArrays(output, expected))
            printf("Passed.
    ");
        else
            printf("FAILED.
    ");
    }
    
    static void test1()
    {
        // 输入数组中没有0
        double input[] = { 1, 2, 3, 4, 5 };
        double output[] = { 0, 0, 0, 0, 0 };
        double expected[] = { 120, 60, 40, 30, 24 };
        vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));
    
        test("Test1", vector<double>(input, input + sizeof(input) / sizeof(double)),
            ouputTemp,
            vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
    }
    
    static void test2()
    {
        // 输入数组中有一个0
        double input[] = { 1, 2, 0, 4, 5 };
        double output[] = { 0, 0, 0, 0, 0 };
        double expected[] = { 0, 0, 40, 0, 0 };
        vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));
    
        test("Test2", vector<double>(input, input + sizeof(input) / sizeof(double)),
            ouputTemp,
            vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
    }
    
    static void test3()
    {
        // 输入数组中有两个0
        double input[] = { 1, 2, 0, 4, 0 };
        double output[] = { 0, 0, 0, 0, 0 };
        double expected[] = { 0, 0, 0, 0, 0 };
        vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));
    
        test("Test3", vector<double>(input, input + sizeof(input) / sizeof(double)),
            ouputTemp,
            vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
    }
    
    static void test4()
    {
        // 输入数组中有正、负数
        double input[] = { 1, -2, 3, -4, 5 };
        double output[] = { 0, 0, 0, 0, 0 };
        double expected[] = { 120, -60, 40, -30, 24 };
        vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));
    
        test("Test4", vector<double>(input, input + sizeof(input) / sizeof(double)),
            ouputTemp,
            vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
    }
    
    static void test5()
    {
        // 输入输入中只有两个数字
        double input[] = { 1, -2 };
        double output[] = { 0, 0 };
        double expected[] = { -2, 1 };
        vector<double> ouputTemp = vector<double>(output, output + sizeof(output) / sizeof(double));
    
        test("Test5", vector<double>(input, input + sizeof(input) / sizeof(double)),
            ouputTemp,
            vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
    }
    
    int main(int argc, char* argv[])
    {
        test1();
        test2();
        test3();
        test4();
        test5();
    
        return 0;
    }
    测试代码

    分析:分解计算步骤,单独处理。

    class Solution {
    public:
        vector<int> multiply(const vector<int>& A) {
            
            vector<int> B;
            int length = A.size();
            if (length < 1)
                return B;
            
            double temp = 1;
            B.push_back(temp);
            for (int i = 1; i < length; ++i)
            {
                temp = B[i - 1] * A[i - 1];
                B.push_back(temp);
            }
            
            temp = 1;
            for (int i = length - 2; i >= 0; --i)
            {
                temp = temp * A[i + 1];
                B[i] = B[i] * temp;
            }
            return B;
        }
    };
    牛客网提交代码
  • 相关阅读:
    基于u盘身份验证
    新的一年开始了~!
    asp.net的条形码
    windows phone (21) Grid元素的Background和Clip
    windows phone (19) 深入了解TextBlock
    windows phone (25) Canvas元素B
    windows phone (20) Image元素
    windows phone (22) 隐藏元素
    windows phone (26) ApplicationBar应用程序栏
    windows phone (27) 基础Button
  • 原文地址:https://www.cnblogs.com/ZSY-blog/p/12695113.html
Copyright © 2011-2022 走看看