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));}