Put the Apples

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.

Enter a description

Each use case consists of two integers m and N. 0<=m<=10,1<=n<=10.

Output description

Outputs a number that represents the total number of methods to put apples.

Input sample

7 3

Output sample

8

Analysis of Problem solving

 Set F (M, N) to M apples, and n plates.

When n > M, there are n-m plates that are always empty, and removing them will not have an effect on the number of apples placed. i.e. if (n > M) f (m, n) = f (m, m);

When M >= N, different methods of discharge can be divided into two categories:

1) There is at least one plate empty, which is equivalent to F (m, n) = (m, n-1);

2) All the plates have at least one apple, then the m-n apples are left on n plates, i.e. f (m, n) = f (m-n, N);

  Conditions for ending recursion

1) n = 1 o'clock, there is only one method, all apples are placed on a plate, so return 1.

2) m = 0 o'clock, indicating that all apples are released and returned 1.

Test code
1#include <stdio.h>2 3 intPutapples (intMintN)4 {5     if(M = =0|| n = =1)6     {7         return 1;8     }9     if(M <N)Ten     { One         returnPutapples (M, m); A     } -     returnPutapples (M, N-1) + Putapples (M-n, N); - } the  - intMainvoid) - { -     intm, N; +      while(SCANF ("%d%d", &m, &n)! =EOF) -     { +printf"%d\n", Putapples (M, n)); A     } at     return 0; -}

Put the Apples

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.