[Huawei machine trial] 70. Apple, Huawei
Question
The number of ways in which M of identical apples are put in N identical baskets. 1 <= M <= 10, 1 <= N <= 10 For example, five apples in three baskets, 3, 1, 1, and 3 are the same way to input 7 3 Output 8
Ideas
Set f (m, n) to m apples and the number of n plates to be placed:
Description of recursive exit conditions:
When n = 1, all the apples must be placed on one plate, so 1 is returned. When no apple can be placed, 1 is defined as a PUT method. There are two recursive paths, the first n will gradually decrease and eventually reach the exit n = 1; the second m will gradually decrease, because when n> m, we will return f (m, m) so it will eventually reach exit m = 0.
Code
/* ------------------------------------- * Date: 2015-07-07 * Author: SJF0115 * Subject: Apple * Source: huawei machine trial question ----------------------------------------- */# include <iostream> # include <string> # include <vector> # include <stack> # include <algorithm> using namespace std; // apple's basket int pair apple (int apple, int basket) {// because we always ask apple> = basket to solve the problem, so apple-basket> = 0, // end with apple = 0. if it is changed to apple = 1, the correct solution may not be obtained if (apple = 0 | basket = 1) {return 1 ;} // if // more than apple, else if (apple <basket) {return then apple (apple, apple);} // else return then apple (apple, basket-1) + Alibaba apple (apple-basket, basket);} int main () {int apple, basket; // freopen ("C: \ Users \ Administrator \ Desktop \ acm.txt "," r ", stdin); while (cin> apple> basket) {cout <apple, basket) <endl;} // while return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.