《剑指offer》第六十六题(构建乘积数组)

时间:2023-03-09 19:08:56
《剑指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 > )//还是要边界判断一下
{
output[] = ;
for (int i = ; i < length1; ++i)//计算左面上三角矩阵的每行乘积值
{
output[i] = output[i - ] * input[i - ];
} double temp = ;
for (int i = length1 - ; i >= ; --i)//注意两个循环的i初始化值
{
temp *= input[i + ];//计算下三角矩阵的每行乘积值
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 = ; 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[] = { , , , , };
double output[] = { , , , , };
double expected[] = { , , , , };
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[] = { , , , , };
double output[] = { , , , , };
double expected[] = { , , , , };
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[] = { , , , , };
double output[] = { , , , , };
double expected[] = { , , , , };
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[] = { , -, , -, };
double output[] = { , , , , };
double expected[] = { , -, , -, };
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[] = { , - };
double output[] = { , };
double expected[] = { -, };
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 ;
}