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;}