POJ Apple Problem (recursive)

Source: Internet
Author: User

First we imagine a function count (m,n) that can put m apples on n plates.

The relationship between N and M can be further analyzed:

Special M-<=1| | n <= 1 O'Clock there is only one method;

When M < n, even if Apple does not have one plate to fill all the plates, the topic allows the tray to be empty, so we can remove the empty plate, that is, COUNT (m, N) = count (M, m);

When M >= N, there are two cases:

n a plate with an empty plate, when there are empty plates, count (m, N) = count (M, n-1);

There are no empty plates on the n plates, which means that there is at least one apple on each plate, filling all the plates, and leaving m-n an apple, so now the question becomes how many ways the m-n apples put on n plates, that is, count (M-n, N).

So when m>=n, the total condition for placing apples is count (M, n-1) + count (m-n, N) times.

The specific code is implemented as follows:

#include <iostream>using namespacestd;intCountintMintN) {    if(M <=1|| N <=1)         return 1; if(M <N)returncount (M, m); Else        returnCount (M, N-1) + count (M-n, n);}intMain () {intm, N; CIN>> m >>N; cout<< count (M, N) <<Endl; return 0;}

POJ Apple Problem (recursive)

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.