构建乘积数组-C++

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程icon-default.png?t=N7T8https://www.captainai.net/

// 面试题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)
{
    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)
        {
            temp *= input[i + 1];
            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.\n");
    else
        printf("FAILED.\n");
}

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> tmp = vector<double>(output, output + sizeof(output) / sizeof(double));
    test("Test1", vector<double>(input, input + sizeof(input) / sizeof(double)),
         tmp,
         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> tmp = vector<double>(output, output + sizeof(output) / sizeof(double));
    test("Test2", vector<double>(input, input + sizeof(input) / sizeof(double)),
         tmp,
         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> tmp = vector<double>(output, output + sizeof(output) / sizeof(double));
    test("Test3", vector<double>(input, input + sizeof(input) / sizeof(double)),
         tmp,
         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> tmp = vector<double>(output, output + sizeof(output) / sizeof(double));
    test("Test4", vector<double>(input, input + sizeof(input) / sizeof(double)),
         tmp,
         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> tmp = vector<double>(output, output + sizeof(output) / sizeof(double));
    test("Test5", vector<double>(input, input + sizeof(input) / sizeof(double)),
         tmp,
         vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}

int main(int argc, char *argv[])
{
    test1();
    test2();
    test3();
    test4();
    test5();

    return 0;
}

// ------ Output ------
/*
Test1 Begins: Passed.
Test2 Begins: Passed.
Test3 Begins: Passed.
Test4 Begins: Passed.
Test5 Begins: Passed.
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值