[Huawei machine trial] 70. Apple, Huawei

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.