Divide an integer into the sum of multiple positive integers.

Source: Internet
Author: User
Tags integer division
The problem of integer division is to split a positive integer n into a group of numbers that are joined and equal to n. Obviously, the maximum number of integers in this group is not greater than n. Let n be the integer to be divided, and m be the largest integer after division. For example, dividing 6 into a maximum addition Number of 6 is as follows: 65 + 14 + 2, 4 + 1 + 13 + 3, 3 + 2 + 1 + 1 + 12 + 2 + 2, 2 + 2

The problem of integer division is to split a positive integer n into a group of numbers that are joined and equal to n. Obviously, the maximum number of integers in this group is not greater than n. Let n be the integer to be divided, and m be the largest integer after division. For example, divide 6 into 6 with the maximum number of processors as follows: 65 + 14 + 2, 4 + 1 + 13 + 3, 3 + 2 + 1, 3 + 1 + 1 + 12 + 2 + 2, 2 + 2

The problem of integer division is to split a positive integer n into a group of numbers that are joined and equal to n. Obviously, the maximum number of integers in this group is not greater than n.

Let n be the integer to be divided, and m be the largest integer after division. For example, dividing 6 into a maximum addition Number of 6 is as follows:

65 + 14 + 2, 4 + 1 + 13 + 3, 3 + 2 +1, 3 + 1 + 1 + 12 + 2 + 2, 2 + 2+ 1 + 1, 2 + 1 + 1 + 1 + 11 + 1 + 1 + 1 +1 + 1

A total of 11 division methods. If the maximum integer after division is 2, the Division form is the last two rows, a total of four division methods. It is easy to use and can be solved using recursion. The Division function split (int n, int m) is set, where n is the integer to be divided, and m is the maximum addition after division.

(1) m <1 or n <1, there are 0 partitioning methods.

(2) When m = 1 or n = 1, there are 1 division methods.

(3) When n is less than m, because the largest addition in integer division cannot be greater than n, this is equivalent to split (n, n ).

(4) When m = n, there are two types of division: one is the maximum number of integers expressed as m-1, the total split (n, m-1; one is m (of course there is no other addition, or another addition is 0 ). Therefore, m = n is split (m-1) + 1 division method.

(5) m <n, there are also two types of division: one is the maximum number of m-1, a total of split (m-1 ); the other is m, followed by dividing n-m and the maximum addition is m. Therefore, the total split (n-m, m) method. Therefore, m <n is divided in a total of split (m-1) + split (n-m, m.

The source code is as follows:

#include 
 
  int split(int n, int m){   if(n < 1 || m < 1) return 0;   if(n == 1 || m == 1) return 1;   if(n < m) return split(n, n);   if(n == m) return (split(n, m - 1) + 1);   if(n > m) return (split(n, m - 1) + split((n - m), m));}
 

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.