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:
6
5 + 1
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 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 <stdio. h> 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 ));}
Divide an integer into the sum of multiple positive integers.