"Sword Point offer": [52] Building a product array

Source: Internet
Author: User

title: Given an array of a[0,1,2...n-1], build an array b[0,1,2,... N-1], so that the element in B is b[i]=a[0]*a[1]*...*a[i-1]*a[i+1]*...*a[n-1], you cannot use division.
Solution Ideas:Through the positive and negative two times to seek b[i];
Zheng: b[i]=a[0]* ... A[I-1];
Inverse: temp = b[n]* ... B[I+1];
Finally, the results can be obtained by b[i]*temp.
Take a[0,1,2,3,4], b[0,1,2,3,4] For example:
Step Analysis:
First step: b[0] = 1;
Step two: b[1] = b[0] * A[0] = a[0]
Step three: b[2] = b[1] * A[1] = a[0] * a[1];
Fourth step: b[3] = b[2] * a[2] = a[0] * a[1] * a[2];
Fifth step: b[4] = b[3] * A[3] = a[0] * a[1] * a[2] * a[3];
Then for a second for loop
The first step:
Temp *= a[4] = 1*a[4];
B[3] = b[3] * temp = a[0] * a[1] * a[2] * a[4];
Step Two:
Temp *= a[3] = a[4] * A[3];
B[2] = b[2] * temp = a[0] * a[1] * a[4] * a[3];
Step Three:
Temp *= a[2] = a[4] * a[3] * a[2];
B[1] = b[1] * temp = a[0] * a[4] * a[3] * a[2];
Fourth Step:
Temp *= a[1] = a[4] * a[3] * a[2] * a[1];
B[0] = b[0] * temp = a[4] * a[3] * a[2] * a[1];
The specific implementation code is as follows:
#include <iostream>using namespace std; #include <vector>vector<int> array1;vector<int> Array2 (5); void Multiply (const vector<int> &array1,vector<int> &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;}}} int main () {for (int i=0;i<5;i++) array1.push_back (i+1); multiply (array1,array2); Vector<int>::iterator it1; Vector<int>::iterator it2;cout<< "A array is:"; for (It1=array1.begin (); It1!=array1.end (); it1++) cout<< *it1<< "";cout<<endl;cout<< "b array is:"; for (It2=array2.begin (); It2!=array2.end (); it2++) cout< <*it2<< ""; Cout<<endl;system ("pause"); return 0;}

The main use of intermediate result variables. Operation Result:


"Sword Point offer": [52] Building a product array

Contact Us

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.