Integer decomposition with the largest Product

Source: Internet
Author: User

1. Problem Description

Divide 2010 into the sum of several non-identical positive integers to maximize the product of these non-identical positive integers.

 

2. Key Points of Design

After generalization, the specified positive integer N is divided into several integers that are different from each other, so that the product of these integers is the largest.

In the zero decomposition for maximizing the product, the minimum zero number is C and the maximum zero number is D.

(1) c> 1. If C = 1, remove Zero 1 and Add 1 to the maximum zero number. Obviously, the product increases.

(2) The number of zeros is arranged in ascending order. In the zero number sequence from C to D, the number of null values in the middle (not in the zero number sequence) cannot exceed 1.

There are two null numbers x and y in the sequence, which meet the requirements of a (I) <x <Y <A (J), where a (I), A (j) it is the item (I <j), x = a (I) + 1, y = a (j)-1 in the zero number sequence. Because a (I) + a (j) = x + y

A (j)> A (I) + 1 => X * Y = (a (I) + 1) * (A (j)-1)> A (I) * A (j)

That is to say, after the zero numbers a (I) and a (j) are replaced with X and Y respectively, and the product increases without changing. This is the biggest contradiction between them and each person.

(3) c <4, that is, C can only take 2 and 3.

If C = 4, if 5 is in the sequence, convert 5 to 2 + 3, and the product increases. If 5 is not in the sequence and 6 is in the sequence, convert 4 and 6 to 2, 3, and 5. Obviously, the product increases without changing. If 5 and 6 are not in the sequence, they are in conflict with the above (2.

If C> 4, turn C into 2 and C-2, obviously 2 (C-2)> C, product increase.

Therefore, convert the specified n to a continuous or at most one null positive integer Sequence starting with 2 or 3, and the corresponding product reaches the maximum.

 

3. Code Implementation

# Include "stdafx. H "<br/> # include <math. h> </P> <p> // integer decomposition program with the largest product <br/> int main (void) <br/>{< br/> int, c, D, H, k, n, s; <br/> double T; </P> <p> printf ("splits positive integer N into the sum of several distinct positive numbers to maximize the product. /N "); <br/> printf (" enter a positive integer N: "); <br/> scanf (" % d ", & N ); </P> <p> S = 0; h = 0; A = 1; <br/> while (S <= N) {// at this time, S-N may be 1, 2 ,..., A <br/> A ++; <br/> S + = A; <br/>}< br/> If (S-N =) {// n is decomposed into a continuous sequence of 2 to A-1 <br/> C = 2; <br/> d = A-1; <br/>}< br/> else if (S-N = 2) {// n is decomposed into a continuous sequence from 3 to a <br/> C = 3; <br/> d = A; <br/>}< br/> else if (S-N = 1) {// n is decomposed into 3 to a + 1 (excluding a) <br/> C = 3; <br/> d = a + 1; <br/> H = A; <br/>}< br/> else {// n is divided into 2 to A (excluding S-N) <br/> C = 2; <br/> d = A; <br/> H = S-N; <br/>}</P> <p> printf ("% d: % d -- % d", N, C, D ); <br/> If (h> 0) <br/> printf ("(excluding the Number % d)", H ); <br/> printf ("/N has the largest product:"); <br/> T = 1; <br/> for (k = C; k <= D; k ++) <br/> T * = K; <br/> If (h> 0) <br/> T/= h; </P> <p> printf ("%. 0f/N ", T); </P> <p> return 0; <br/>}

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.