Title Description
Put m the same apples on n the same plate, allow some plates to be empty, ask how many different ways? (denoted by k) 5,1,1 and 1,5,1 are the same kind of sub-method.
Enter a description
Each use case consists of two integers m and N. 0<=m<=10,1<=n<=10.
Output description
Outputs a number that represents the total number of methods to put apples.
Input sample
7 3
Output sample
8
Analysis of Problem solving
Set F (M, N) to M apples, and n plates.
When n > M, there are n-m plates that are always empty, and removing them will not have an effect on the number of apples placed. i.e. if (n > M) f (m, n) = f (m, m);
When M >= N, different methods of discharge can be divided into two categories:
1) There is at least one plate empty, which is equivalent to F (m, n) = (m, n-1);
2) All the plates have at least one apple, then the m-n apples are left on n plates, i.e. f (m, n) = f (m-n, N);
Conditions for ending recursion
1) n = 1 o'clock, there is only one method, all apples are placed on a plate, so return 1.
2) m = 0 o'clock, indicating that all apples are released and returned 1.
Test code
1#include <stdio.h>2 3 intPutapples (intMintN)4 {5 if(M = =0|| n = =1)6 {7 return 1;8 }9 if(M <N)Ten { One returnPutapples (M, m); A } - returnPutapples (M, N-1) + Putapples (M-n, N); - } the - intMainvoid) - { - intm, N; + while(SCANF ("%d%d", &m, &n)! =EOF) - { +printf"%d\n", Putapples (M, n)); A } at return 0; -}
Put the Apples