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
Analysis:
Assuming that for the M apples, n plates have Apple (m,n) method, then we should find a way to reduce the value of m,n recursively, first set the recursive condition, when m<=1 or N<=1, apple (m,n) = 1;
Then, the value of the m,n is discussed in two different situations.
(1) If m<n, then there must be a m-n plate is empty, and empty which plate on the result is not affected, this and M-Apple put the number of plates, so Apple (m,n) =apple (m,m).
(2) If m>=n, then in two different cases of discussion: 1) All the dishes have apples on it, then take an apple from each plate has no effect on the results, or understand that when the apples are placed on each plate to put an apple, and then put m-n an apple on the N plate, Put an apple on each plate is the only, and no effect on the results of the subsequent, this situation Apple (M,n) =apple (m-n,m)
2) Not all dishes have apples, in other words, at least one plate is empty, and the empty plate is not affected by the result, Apple (m,n) =apple (m,n-1), this situation, even if there are n plates are empty can also find n times apple (m,n) =apple (M, n-1), but only if at least one of the plates is empty is completely established.
The above has already discussed all the situation, can write the procedure.
#include <iostream>using namespacestd;intAppleintMintN) { if(m<=1|| n<=1) { return 1; } if(m<N) {returnApple (m,m); } Else { returnApple (m,n-1) +apple (M-n,n); }}intMain () {intn,m; CIN>>m>>N; cout<<Apple (m,n); return 0;}
OJ put an apple