OJ put an apple

Source: Internet
Author: User

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

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.