Array product -- satisfies the product of all numbers except input [I] In result [I] = input array (assuming no Overflow

Source: Internet
Author: User

Array product (15 points)
Input: an integer array of n
Output: the result of an integer array with the length of n, which satisfies the product of all numbers except input [I] In result [I] = input array (it is assumed that it will not overflow ). For example, input: input = {2, 3, 4, 5}, output result = {60, 40, 30, 24}
The smaller the program time and space complexity, the better.

                 #include <stdio.h>     pr_arr( arr[],           ( i=;i<n;i++          printf(      printf(    arrayMultiply( input[], result[], n)      (n<=)      result[]=          ( i =;i<n;i++)           result[i]=result[i-]*input[i-       q=     ( i=n-;i>=;--i)           q*=input[i+         result[i]*=     main(      s[]={,,,,,,      n=(s)/(       *result =          }  

 

Input: an integer array of n
Output: the result of an integer array with the length of n, which satisfies the product of all numbers except input [I] In result [I] = input array (it is assumed that it will not overflow ). For example, input: input = {2, 3, 4, 5}, output result = {60, 40, 30, 24}
The smaller the program time and space complexity, the better.
C/C ++:
Int * cal (int * input, int n );

Java:
Int [] cal (int [] input );

int *cal(int* input , int n)   {       int i ;       int *result = new int[n];       result[0] = 1;       for(i = 1 ; i < n ; ++i)           result[i] = result[i-1]*input[i-1];       result[0] = input[n-1];       for(i = n-2 ; i > 0 ; --i)       {           result[i] *= result[0];           result[0] *= input[i];       }       return result;   }   

Test code:

#include<iostream>using namespace std;int *cal(int *input , int n){int i;int *result = new int[n];result[0] = 1;for(i = 1 ; i < n ; ++i){result[i] = result[i - 1] * input[i - 1];}result[0] = input[n - 1];for(i = n - 2 ; i > 0 ; --i){result[i] *= result[0];result[0] *= input[i];}//return result;for(int i = 0 ; i < n ; ++i)cout<<result[i]<<",";cout<<endl;return 0;}int main(){int input[] = {2 , 3 , 4 , 5};int length = sizeof(input) / sizeof(int);*cal(input , length);return 0;}

 

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.