Represent a positive integer N as the sum of a series of positive integers. There are several division methods.

Source: Internet
Author: User

Question: a positive integer N is expressed as the sum of a series of positive integers. There are several division methods.

N = N1 + N2 + N3 +... + NK (N1> = N2> = N3....> = 1, k> = 1)

It is called a division of positive integers. Positive integer N may have different divisions. For example, all the divisions of positive integer 6 are

6 = 6

6 = 5 + 1

6 = 4 + 2 6 = 4 + 1 + 1

6 = 3 + 3 6 = 3 + 2 + 1 6 = 3 + 1 + 1 + 1

6 = 2 + 2 + 2 6 = 2 + 2 + 1 + 1 6 = 2 + 1 + 1 + 1 + 1

6 = 1 + 1 + 1 + 1 + 1 + 1

Method: Find the recurrence formula:

# Include <iostream>
Using STD: cout;
Using STD: Endl;
Using STD: CIN;

Int partition (int abc, int max) // Max indicates the maximum positive integer in the partitioned positive integer
{
If (ABC = 1 | max = 1)
{
Return 1;
}
If (ABC <max)
Return partition (ABC, ABC );
If (ABC = max)
Return partition (ABC, abc-1) + 1;
If (ABC> MAX)
Return partition (ABC, max-1) + partition (ABC-Max, Max); // consists of two parts: one is represented by the max-1; the other is represented by the Max, it can be converted to the ABC-Max representation type.
}

Int main ()
{
Int ABC;
While (CIN> ABC)
Cout <partition (ABC, ABC) <Endl;
}

 

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.