Given array a[n] construct array b[n]

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/wumuzi520/article/details/7841280

Given an array of a[n], we want to construct array b [N],
where b[j]=a[0]*a[1]...a[n-1]/a[j],
Division is not allowed during construction: requires O (1) space complexity and O (n) time complexity;
The new variable cannot be used except to traverse the counter with a[n] b[n]
(including Stack temp variables, heap space and global static variables, Etc.)

Resolution: Set B[0]=1
Available from b[i]=b[i-1]*a[i-1]
b[1] = a[0]
b[2] = a[0]a[1]
...
b[i] = a[0]a[1]a[2]...a[i-1]
...
b[n-1] = a[0]a[1]...a[n-2]
Then iterate through the variable b[0]
1, a[n-1], a[n-2]a[n-1], a[n-3]a[n-2]a[n-1], ..., a[1]a[2]a[3]...a[n-1],
Iterative process multiplied by b[n-1], b[n-2], ..., b[0]

1 /*************************************************************************2 > File Name: the given array a[n] constructs an array b[n].c3 > Author:juntaran4 > Mail: [email protected]5 > Created time:2016 August 24 Wednesday 16:47 25 seconds6  ************************************************************************/7 8  9  /*Ten given an array of a[n], we want to construct array b [N], one where b[j]=a[0]*a[1]...a[n-1]/a[j], a Division is not allowed during construction: requires O (1) space complexity and O (n) time complexity; - the new variable cannot be used except to traverse the counter with a[n] b[n] - (including Stack temp variables, heap space and global static variables, etc.) the   - resolution: Set B[0]=1 - available from b[i]=b[i-1]*a[i-1] - b[1] = a[0] + b[2] = a[0]a[1] - ... .. + b[i] = a[0]a[1]a[2]...a[i-1] a ... .. at b[n-1] = a[0]a[1]...a[n-2] - then iterate through the variable b[0] - 1, a[n-1], a[n-2]a[n-1], a[n-3]a[n-2]a[n-1], ..., a[1]a[2]a[3]...a[n-1], - iterative process multiplied by b[n-1], b[n-2], ..., b[0] -   -  */ in  -#include <stdio.h> to  + voidTranslate (inta[],intb[],intN) - { theb[0] =1; *      $      for(inti =1; I < n; ++I)Panax Notoginseng     { -b[i] = b[i-1] * a[i-1]; the     } +      a      for(inti =0; I < n; ++I) the     { +printf"b[%d] is%d\n", i, b[i]); -     } $printf"\ n"); $      -      for(inti = n1; I >=1; --I) -     { theb[i] *= b[0]; -b[0] *=a[i];Wuyi     } the      -      for(inti =0; I < n; ++I) wu     { -printf"b[%d] is%d\n", i, b[i]); about     } $printf"\ n"); - } -  - intMain () a { +     inta[] = {2,3,4,5}; the     intb[] = {0,0,0,0}; -Translate (a, b,4); $      the      for(inti =0; I <4; ++I) the     { theprintf"%4d", a[i]); the     } -printf"\ n"); in      the      for(inti =0; I <4; ++I) the     { aboutprintf"%4d", b[i]); the     } theprintf"\ n"); the}

Given array a[n] construct array b[n]

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.