This is a common solution of number division.
I didn't understand the above solution. I didn't understand it at all. How could I think of recursion like this? I couldn't help my IQ too low.
But it doesn't matter. I have racked my brains to think of a method, even though it is common to build a tracing tree. Although it consumes some extra space, it is not bad, and the code is simple and useful.
When I calculate the number of partitions in 4, I construct the tree like this: (after only two layers are drawn, we can use the following tree to traverse the sum. If the sum is equal to 4, we divide the number + 1)
Public class huafen {public int num; // Number of partitions to be divided // n indicates the value to be divided, and M indicates the number of layers to be iterated, count indicates the current total public void huafen (int n, int M, int count) {for (; m <= N; m ++) {// if the value is greater than the value we need, go up without going down the system. out. println ("count" + count); If (count + M> N) {} If (count + M = N) {num ++ ;} if (count + M <n) {huafen (n, m, Count + M) ;}} public static void main (string ARGs []) {huafen Huan = new huafen (); huan. huafen (7,1, 0); system. out. println (huan. num );}}
The most understandable number division algorithm