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
9}
Sample output
8
/**
* Calculate the number of Apple-put methods
* Returns when the input value is illegal-1
* 1 <= m,n <= 10<><= m,n <= 10<>
* Number of @param m apples
* @param n Number of plates
* Total number of @return placement methods
*
*/
public static int count (int m, int n)
Ideas:
1. Using recursive thinking, assume that the number of placement combinations of the latter basin has been obtained;
2. Because the title is a combination, so to remove the same repetition of the combination, one way is to put the plate from left to right column, the number of apples in descending order to avoid duplication, so there is a judgment.
3. End condition to determine whether the current is satisfied with the descending order, satisfy that the combination is known to be true, return 1, otherwise return 0.
1#include <iostream>2 using namespacestd;3 4 intCountintMintNintb)5 {6 //End Condition7 if(n==1)8 {9 if(m<=b)Ten return 1; One Else A return 0; - } - the //Recursive - intnum=0; - for(inti=m;i>=0; i--) - { + if(i<=b) -Num=num+count (m-i,n-1, i); + } A returnnum; at } - - intMain () - { - intM,n; -Cin>>m>>N; in intnum=count (m,n,m); -cout<<num<<Endl; to}
In the process of writing this algorithm, I began to calculate the number of permutations first, and then eliminate the repetition. But later found that they did not find how to remove the repeated arrangement of the method. Because some of the number of permutations is different from the number of other permutations. The following is the wrong algorithm.
1#include <iostream>2 3 using namespacestd;4 5 intCountintMintN)6 {7 if(n==1)8 {9 return 1;Ten } One A intnum=0; - for(inti=m;i>=0; i--) - { theNum=num+count (m-i,n-1); - } - returnnum; - } + - intMulintN) + { A intresult=1; at for(inti=n;i>=1; i--) - { -result=result*i; - } - returnresult; - } in - intMain () to { + intM,n; -Cin>>m>>N; the intnum=count (m,n); *num=num/mul (n); $cout<<num;Panax Notoginseng}
[Huawei OJ] put apples