An array of N numbers for the maximum product of the number of N-1 combinations

Source: Internet
Author: User
1/* 2 programming of the beauty of the question, given N number of arrays, can only use multiplication, do not use division, find the maximum number of N-1 product of a group, there are two methods, method 1: Use two arrays to save the product values from left to right 3 and from and to left, and then scan once to find the maximum product and space for time. 4. Method 2: analyze the nature of these numbers to see how many positive numbers, how many negative numbers, how many 0 5 (1) if there are two or more 0, returns 0 6 (2). If there is a 0 value, the product after 0 is removed is positive or negative. If it is negative, the maximum value is 0, returns 0 (that is, if there is a 0 value, it indicates the Negative Parity. If the negative number is an odd number, it returns 0) 7. If it is a positive number, returns the positive number (that is, all other products) 8 (3). If the total product is negative, the negative is positive, and the negative number of the smallest absolute value is removed. 9 (4) if the total product is positive, if a positive number exists, a positive number with the smallest absolute value is removed; otherwise, a negative number with the maximum absolute value is removed. 10 because the question does not allow division, it cannot directly multiply the total product and then use division. Therefore, a scan is required to record the number of positive numbers, number of negative numbers, minimum positive numbers of absolute values, and negative numbers of absolute values, the negative value of the absolute value. 11 */12 13 # include <iostream> 14 using namespace STD; 15 16 double maxmultiply (const double * a, int N) 17 {18 if (a = NULL) 19 Return 0; 20 double * Left = new double [N]; 21 double * Right = new double [N]; 22 left [0] = A [0]; 23 right [n-1] = A [n-1]; 24 for (INT I = 1; I <n; I ++) 25 left [I] = left [I-1] * A [I]; 26 for (INT I = n-2; I> = 0; I --) 27 right [I] = right [I + 1] * A [I]; 28 double result = A [0]; 29 result = max (left [N-2], right [1]); // when you first judge two boundaries, you do not need to compare them later. 30 For (INT I = 1; I <= n-2; I ++) 31 {32 result = max (result, left [I-1] * right [I + 1]); 33} 34 return result; 35} 36 37/* 38 method 2, record number of positive and negative numbers 39 */40 double maxmultiply2 (const double * a, int N) 41 {42 if (a = NULL) 43 return 0; 44 int Pos = 0; 45 int neg = 0; 46 int zero = 0; 47 double fabminp = A [0]; // positive number with the smallest absolute value 48 double fabminn = A [0]; // negative number with the smallest absolute value 49 double fabmaxn = A [0]; // negative value of the absolute value 50 double result = 1; 51 for (INT I = 0; I <n; I ++) 52 {53 if (a [I] = 0) 54 zero ++; 55 if (a [I]> 0) 56 {57 POS ++; 58 if (a [I] <fabminp) 59 fabminp = A [I]; 60} 61 if (a [I] <0) 62 {63 neg ++; 64 if (a [I] * (-1) <fabminn) 65 {66 fabminn = A [I]; 67} 68 if (a [I] * (-1)> fabmaxn) 69 {70 fabmaxn = A [I]; 71} 72} 73} 74 if (zero> = 1) 75 {76 if (zero> = 2) // if there are more than two zeros, 0 is returned. 77 return 0; 78 else if (NEG & 1) = 0) // There Is A 0, and the number of negative numbers is an even number, indicating that the product is a positive number, return this positive number, 79 {80 for (INT I = 0; I <n; I ++) 81 {82 if (a [I]! = 0) 83 result * = A [I]; 84} 85 return result; 86} 87 else return 0; // There Is A 0, and the other products are negative, the maximum value is 0. 88} 89 else if (NEG & 1) = 0) // if the number of negative numbers is an even number (including 0), the total product is positive, 90 {91 If (Pos> 0) must be enclosed in parentheses. // if there is a positive number, remove the smallest positive number, such as 2, 3, 4,-1,-2, remove 2, if 3,-1,-2, remove 3 92 {93 for (INT I = 0; I <n; I ++) 94 {95 if (a [I]! = Fabminp) 96 result * = A [I]; 97} 98 return result; 99} 100 else // if there is no positive number, it is an even number, for example,-2,-3, -4,-5, remove the negative number of the absolute value 101 {102 for (INT I = 0; I <n; I ++) 103 {104 if (a [I]! = Fabmaxn) 105 result * = A [I]; 106} 107 return result; 108} 109 else // The number of negative numbers is an odd number, and the total product is a negative number such as 2, 3, -1,-2,-3, remove the negative number with the smallest absolute value,-1 111 {112 for (INT I = 0; I <n; I ++) 113 {114 if (a [I]! = Fabminn) 115 result * = A [I]; 116} 117 return result; 118} 119} 120 121 122 int main () 123 {124 double A [] =, -125, 126, 127,-3}; int n = 9; cout <maxmultiply2 (A, n) <Endl; System ("pause "); 128}

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.