# Build Product array __ Build product Array

Source: Internet
Author: User

Question: Given an array a[0,1,..., n-1], build an array b[0,1,..., n-1], where the element in B b[i]=a[0]*a[1]*.....*a[i-1]*a[i+1]*....*a[n-1].

Algorithmic thinking: The B[i] is regarded as the c[i]=a[0]*a[1]*.....*a[i-1],d[i]=a[i+1]*....*a[n-1 of the product of the two parts of C[i],d[i].

```#include <vector> #include <stdio.h> using namespace std; The solution to divide the product of two arrays void multiply (const vector<double>& array1, vector<double>& array2) {int length1=
Array1.size ();

int length2 = Array2.size ();
if (length1 = = Length2 && length2 > 1) {array2[0] = 1;
for (int i = 1; i < length1 ++i) {array2[i] = array2[i-1] * Array1[i-1];
Double temp = 1;
for (int i = Length1-2 i >= 0; i.) {temp *= array1[i + 1];
Array2[i] *= temp; }}//================= Test Code ================= static bool Equalarrays (const vector<double>& ARR
Ay1, const vector<double>& array2) {int length1= array1.size ();

int length2 = Array2.size ();

if (length1!= length2) return false; for (int i = 0; i < length1 ++i) {if (ABS (Array1[i)-array2[i]) > 0.0000001) return false;
return true; static void Test (char* testname, const vector<double>& array1, vector<double>& array2, const vector

<double>& expected) {printf ("%s begins:", testname);
Multiply (array1, array2);
if (Equalarrays (array2, expected)) printf ("passed.\n");
else printf ("failed.\n");
} static void Test1 () {double array1[] = {1, 2, 3, 4, 5};
Double array2[] = {0, 0, 0, 0, 0};

Double expected[] = {120, 60, 40, 30, 24}; Test ("Test1", vector<double> (array1, array1 + sizeof (array1)/sizeof (double)), vector<double> (array2 , Array2 + sizeof (array2)/sizeof (double), vector<double> (expected, expected + sizeof (expected)/sizeof (
Double));
} static void Test2 () {double array1[] = {1, 2, 0, 4, 5};
Double array2[] = {0, 0, 0, 0, 0};

Double expected[] = {0, 0, 40, 0, 0}; Test ("Test2", VEctor<double> (array1, array1 + sizeof (array1)/sizeof (double)), vector<double> (array2, array2 + size
Of (Array2)/sizeof (double), vector<double> (expected, expected + sizeof (expected)/sizeof (double));
} static void Test3 () {double array1[] = {1, 2, 0, 4, 0};
Double array2[] = {0, 0, 0, 0, 0};

Double expected[] = {0, 0, 0, 0, 0}; Test ("Test3", vector<double> (array1, array1 + sizeof (array1)/sizeof (double)), vector<double> (array2 , Array2 + sizeof (array2)/sizeof (double), vector<double> (expected, expected + sizeof (expected)/sizeof (
Double));
} static void Test4 () {double array1[] = {1,-2, 3,-4, 5};
Double array2[] = {0, 0, 0, 0, 0};

Double expected[] = {120,-60, 40,-30, 24}; Test ("Test4", vector<double> (array1, array1 + sizeof (array1)/sizeof (double)), vector<double> (array2
, Array2 + sizeof (array2)/sizeof (double)),   Vector<double> (expected, expected + sizeof (expected)/sizeof (double));
} static void Test5 () {double array1[] = {1,-2};
Double array2[] = {0, 0};

Double expected[] = {-2, 1}; Test ("Test5", vector<double> (array1, array1 + sizeof (array1)/sizeof (double)), vector<double> (array2 , Array2 + sizeof (array2)/sizeof (double), vector<double> (expected, expected + sizeof (expected)/sizeof (
Double));
} void Main () {test1 ();
Test2 ();
Test3 ();
Test4 ();
Test5 (); }```
`  `
Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.