The integer division problem is to split a positive integer N into a group of numbers plus N, and the maximum number of integers in this group is not greater than N.
For example, 6 is divided into Integers
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 types. The following describes a recursive method to obtain the number of partitions of a positive integer.
The declaration of recursive functions is int split (int n, int m), where N is the positive integer to be divided, and m is the maximum addition in the Division (when m> N, n ),
1 When n = 1 or M = 1, the split value is 1. As shown in the preceding example, only one partition is 1 or 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
If (n = 1 | M = 1) return 1;
2. Let's take a look at the relationship between M and N. They have three relationships.
(1) m> N
In integer division, the maximum addition number cannot be greater than N. Therefore, this can be equivalent to split (N, N );
Available programs are represented as if (M> N) return split (N, N );
(2) M = N
In this case, it can be recursively expressed as split (n, m-1) + 1. From the above example, we can see that
The sum of the numbers 6 and less than 6
Expressed as if (M = N) Return (split (n, m-1) + 1) using a program );
(3) M <n
This is the most common case.
As shown in the preceding example, if M is set to 4, the split (6, 4) value is the sum of the number of splits whose maximum number is less than 4 and integer 2.
Therefore, split (n, m) can be expressed as split (n, m-1) + split (n-M, m)
Based on the above description, the source code is as follows:
# Include <stdio. h> <br/> int split (int n, int m) <br/> {<br/> If (n <1 | M <1) return 0; <br/> If (n = 1 | M = 1) return 1; <br/> If (n <m) return split (N, N ); <br/> If (n = m) Return (split (n, m-1) + 1); <br/> If (n> m) return (split (n, m-1) + split (n-m), m); <br/>}< br/> int main () <br/>{< br/> printf ("12 Division: % d", split (12, 12); <br/> return 0; <br/>}
Divide a positive integer into the sum of consecutive positive integers.
For example, 15 can be divided into four forms of continuous integer addition:
15
7 8
4 5 6
1 2 3 4 5
First consider the general form, set N to the positive integer to be divided, and X to the smallest integer after division. If n has a division, then
The result is X. If there are two types of division, X and x + 1. If there are m types, is x, x + 1, x + 1 x + 2 ,..., x + 1 x + 2... X + m-1
Add each result to obtain a formula (I * x + I * (I-1)/2) = n, and I is the number of integers added after the current division.
The Division that satisfies the condition is all the conditions that make X a positive integer.
In the above example, when I = 1, it is divided into a positive integer, x = 15, and when I = 2, x = 7.
If X = 3, x = 4, and X = 4, 4/9 is not a positive integer. Therefore, 15 cannot be divided into four integers.
When x = 5, x = 1.
There is another question: what is the maximum I value? However, it must be smaller than N. We can make a hypothesis,
Assume that N can be split into a division with a minimum value of 1. In the preceding example, 1 2 3 4 5. This is the maximum number of N. If this assumption is not met,
Then I must be smaller than the number of positive integers in this division. Therefore, we can obtain the formula I * (I + 1)/2 <= N, that is, when I satisfies
In this formula, N can be divided.
Based on the above, the source program is as follows:
Int split1 (int n) <br/>{< br/> int I, j, m = 0, x, T1, T2; <br/> // here I + 1 is changed to I-1 because I * (I-1)/2 is used multiple times below, <br/> // to avoid repeated calculation, the value is saved in T1 after calculation. And change <= to <. <Br/> for (I = 1; (T1 = I * (I-1)/2) <n; I ++) <br/>{< br/> T2 = (n-t1); <br/> X = T2/I; <br/> If (x <= 0) break; <br/> If (n-t1) % I = 0) <br/> {<br/> printf ("% d", X ); <br/> for (j = 1; j <I; j ++) <br/> printf ("% d", x + J ); <br/> printf ("/N"); <br/> M ++; <br/>}< br/> return m; <br/>}