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.
Input
Each use case consists of two integers m and N. 0<=m<=10,1<=n<=10. <=n<=10<=m<=10
Sample input
3 ·
Sample output
8
Topic Analysis:
The topic is actually a division problem, with the help of recursive this powerful tool, we can be easier to solve, concrete analysis ideas are as follows:
1, we use F (M, N) to represent the case of M apples, n baskets;
2, export conditions: when M <= 1 o'clock F (m, n) = 1, when n = 1 o'clock F (m, n) = 1;
3, when n > m, no matter how to place, there will always be (n-m) a basket is empty, then the removal of these baskets would not have any effect on the total placement type, i.e. f (m, n) = f (m, m);
When n <= m, it is divided into two cases:
3-1, all baskets will not be empty, at this time equivalent to each basket will have at least one apple, then remove these apples, the total placement of the species will not change, that is, F (m, n) = f (m-n, N);
3-2, at least one basket will not be empty, then remove the basket, the total placement of the species will not change, that is, F (m, n) = f (m, m-1);
Then the total number of places is added.
4, according to the analysis in 3, the recursive process F (m, n) parameter 1 and the parameter 2 will be continuously reduced, will certainly reach the export conditions, in line with the conditions of recursion.
Code (Java Implementation):
1 ImportJava.util.Scanner;2 3 Public classLayapple {4 Public Static voidMain (string[] args) {5 int[] Paras =getinput ();6System.out.println (Findnumsoflaying (paras[0), paras[1]));7 }8 Public Static int[] GetInput () {9@SuppressWarnings ("Resource")TenScanner reader =NewScanner (system.in); One int[] Paras =New int[2]; AParas[0] =reader.nextint (); -PARAS[1] =reader.nextint (); - returnparas; the } - - Public Static intFindnumsoflaying (intMintN) { - if(m = = 0 | | n = = 1) { + return1; - } + if(N >m) { A returnfindnumsoflaying (M, m); at}Else { - returnFindnumsoflaying (M-n, N) + findnumsoflaying (M, n-1); - } - } -}
Huawei OJ Apple