Integer partition/Cut (integer partion), and output results, Java, Python__python

Source: Internet
Author: User
Tags integer division

Integer partitioning is a more typical recursive problem: dividing an integer n n n into a series of numbers that are not more than M m.
For example, if n=5 n = 5 n=5, m=4 m = 4 m=4, the partition can be: {4,1}, {3,1,1}, {3,2}, {2,1,1,1}, {2, 2, 1},{1,1,1,1,1}. A total of 6 combinations.

If as long as the total number of output division, online reference more, also easy to understand parity.
If you also require the specific results of the division of output, online data is relatively small, I myself from the foreign website search, found an answer, the same use of recursion thought, mainly:
A string argument is added to the function, and when n-n values drop to 0 o'clock, the line wraps .

Java code:

/** * @author Chen Zhen * @version creation Date: April 25, 2018 PM 4:51:23 * @value class Description: Integer partition, and output/public class Integerpartion {
        Query division number static int countpartion (int n, int m) {if (n ==1 | | | = = 1) return 1;
        else if (M > N) return countpartion (n, N);
        else if (M ==n) return 1 + countpartion (n, n-1);
        else if (M < n) return Countpartion (N-m, m) + countpartion (n, m-1);
    return 0; }//Output specific division static void Countoutput (int n, int m, String str) {if (n = = 0)//recursive jump out of condition 1 System
        . out.println (str);
            else {if (M > 1)///recursive jump out of Condition 2 countoutput (n, m-1, str);
        if (M <= N)///Because the m>n situation may occur when recursion Countoutput (N-m, M, M + "" + str);
        } public static void Main (string[] args) {int m = 5;
        int n = 6;
        System.out.println (Countpartion (M, n));
  Countoutput (M, N, "");  }
}
 

Python Code:

# output integer partition number
def f1 (n, m):
    if n==0 or M ==0: return
        0
    if n==1 or M ==1: return
        1
    if m = = N:        
        ret Urn 1 + F1 (n, n-1)
    elif m > N: Return
        F1 (n, N)
    elif m < n: return
        F1 (n-m, m) + F1 (n, m-1)

# Output Specific integer Division
def f2 (n, M, string):
    If n = 0:
        print (String)
    else:
        if m>1:
            f2 (N, m-1, string
        if M <= N:
            F2 (n-m, M, str (m) + ' +string) 

n = 5 m = 4
print (F1 (n, m))
F2 (n, M, ")

Output results:
6
1 1 1 1 1
1 1 1 2
1 2 2
1 1 3
2 3
1 4

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.