First we imagine a function count (m,n) that can put m apples on n plates.
The relationship between N and M can be further analyzed:
Special M-<=1| | n <= 1 O'Clock there is only one method;
When M < n, even if Apple does not have one plate to fill all the plates, the topic allows the tray to be empty, so we can remove the empty plate, that is, COUNT (m, N) = count (M, m);
When M >= N, there are two cases:
n a plate with an empty plate, when there are empty plates, count (m, N) = count (M, n-1);
There are no empty plates on the n plates, which means that there is at least one apple on each plate, filling all the plates, and leaving m-n an apple, so now the question becomes how many ways the m-n apples put on n plates, that is, count (M-n, N).
So when m>=n, the total condition for placing apples is count (M, n-1) + count (m-n, N) times.
The specific code is implemented as follows:
#include <iostream>using namespacestd;intCountintMintN) { if(M <=1|| N <=1) return 1; if(M <N)returncount (M, m); Else returnCount (M, N-1) + count (M-n, n);}intMain () {intm, N; CIN>> m >>N; cout<< count (M, N) <<Endl; return 0;}
POJ Apple Problem (recursive)