《剑指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 <iostream>
#include <vector>

using namespace std;
//把B[i]看成[=A[0],A[1],… ,A[i-1],1,A[i+1],…,A[n-1]]
//对于B,就成了二维数组,对于1左面是上三角矩阵,右面是下三角矩阵
//三角矩阵的每行乘积值计算可以从顶向下
void BuildProductionArray(const vector<double>& input, vector<double>& output)
{
    int length1 = input.size();
    int length2 = output.size();

    if (length1 == length2 && length2 > 1)//还是要边界判断一下
    {
        output[0] = 1;
        for (int i = 1; i < length1; ++i)//计算左面上三角矩阵的每行乘积值
        {
            output[i] = output[i - 1] * input[i - 1];
        }

        double temp = 1;
        for (int i = length1 - 2; i >= 0; --i)//注意两个循环的i初始化值
        {
            temp *= input[i + 1];//计算下三角矩阵的每行乘积值
            output[i] *= temp;//上下三角的同行乘机值再相乘,就是满足题意的B[i]值了
        }
    }
}

//================= 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> output1= vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test1", vector<double>(input, input + sizeof(input) / sizeof(double)),
        output1,
        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> output1 = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test2", vector<double>(input, input + sizeof(input) / sizeof(double)),
        output1,
        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> output1 = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test3", vector<double>(input, input + sizeof(input) / sizeof(double)),
        output1,
        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> output1 = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test4", vector<double>(input, input + sizeof(input) / sizeof(double)),
        output1,
        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> output1 = vector<double>(output, output + sizeof(output) / sizeof(double));

    test("Test5", vector<double>(input, input + sizeof(input) / sizeof(double)),
        output1,
        vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

int main(int argc, char* argv[])
{
    test1();
    test2();
    test3();
    test4();
    test5();
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/CJT-blog/p/10550206.html