forked from zhedahht/CodingInterviewChinese2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstuctArray.cpp
More file actions
144 lines (118 loc) · 4.06 KB
/
ConstuctArray.cpp
File metadata and controls
144 lines (118 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指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)
{
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(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 };
test("Test1", vector<double>(input, input + sizeof(input) / sizeof(double)),
vector<double>(output, output + sizeof(output) / sizeof(double)),
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 };
test("Test2", vector<double>(input, input + sizeof(input) / sizeof(double)),
vector<double>(output, output + sizeof(output) / sizeof(double)),
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 };
test("Test3", vector<double>(input, input + sizeof(input) / sizeof(double)),
vector<double>(output, output + sizeof(output) / sizeof(double)),
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 };
test("Test4", vector<double>(input, input + sizeof(input) / sizeof(double)),
vector<double>(output, output + sizeof(output) / sizeof(double)),
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}
static void test5()
{
// 输入输入中只有两个数字
double input[] = { 1, -2 };
double output[] = { 0, 0 };
double expected[] = { -2, 1 };
test("Test5", vector<double>(input, input + sizeof(input) / sizeof(double)),
vector<double>(output, output + sizeof(output) / sizeof(double)),
vector<double>(expected, expected + sizeof(expected) / sizeof(double)));
}
int main(int argc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
return 0;
}